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.
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().
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.
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.
I found a way to solve the Problem:
You have to type the following snippet into your .pro file.
DEFINES += Q_COMPILER_INITIALIZER_LISTS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With