Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define static QStringList directly

I want to create and define a static QStringList in an external file. With gcc ist is possible to do it like this:

static QStringList list1 = {item1, item2, item4, ...};

But with the visualStudio c++ compiler it is not possible to do it this way. I get the error:

initializer-list can not be convertedinto QStringList

For me it is important, that I can define the list directly after the declaration.

Because I dont want to define it in the main file.

For example:

main.cpp:

#include "stringlist.cpp"    
int main()
{
    QList<QStringList> list;
    list << list1;
}
...

stringlist.cpp:

#include <QStringList>
static QStringList list1 = {"hi", "hello"};

I want to do that because the definition of the QStringList is very long and it is very confusing if such a big definition is somewhere between the other code.

like image 554
NelDav Avatar asked Jan 11 '19 13:01

NelDav


People also ask

How do you declare a QStringList?

QStringList::QStringList(const QString &str) Constructs a string list that contains the given string, str. Longer lists are easily created like this: QStringList longerList = (QStringList() << str1 << str2 << str3); See also append().

What is a QStringList?

The QStringList class provides a list of strings. QStringList inherits from QList<QString>. Like QList, QStringList is implicitly shared. It provides fast index-based access as well as fast insertions and removals. Passing string lists as value parameters is both fast and safe.

What is QList CPP?

QList<T> is one of Qt's generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.


1 Answers

I found a way to solve the Problem:

You have to type the following snippet into your .pro file.

DEFINES += Q_COMPILER_INITIALIZER_LISTS
like image 64
NelDav Avatar answered Oct 16 '22 13:10

NelDav