6

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.

1 Answer 1

6

I found a way to solve the Problem:

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

DEFINES += Q_COMPILER_INITIALIZER_LISTS 
Sign up to request clarification or add additional context in comments.

3 Comments

I get this error message: error: non-aggregate type 'QStringList' cannot be initialized with an initializer list, I'm also using QT 5.3.2 and -no-c++11 so I might not be able to utilize this solution. Or I'm doing it wrong.
It's probably not answer-worthy as it's not a QStringList, but I went with static QString foo[5] = {"a", "b", "c", "d", "e"};
@NuclearPeon Im am not sure, but I think, that initializing a list is a thing of c++11. That is the reason, why it does not work. QString foo[5] is not a list. It is an array. And arrays could be initialize in c++ versions before c++11. Please correct me if I am wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.