Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to initialize a QList?

Tags:

c++

qt

qlist

What is the right way to initialize QList? I want to make this code shorter:

QSplitter splitter; QList<int> list; list.append(1); list.append(1); splitter.setSizes(list); 

But when I use initialization from std::list, it doesn't seem be working:

splitter.setSizes(QList<int>::fromStdList(std::list<int>(1, 1))); 

In latter case, the splitter seems to divide in ratio 1:0.

like image 766
msgmaxim Avatar asked Sep 25 '13 07:09

msgmaxim


People also ask

How to initialize QList?

QList<int> list({1, 1}); You can enable the latter with the -std=c++0x or -std=c++11 option for gcc. You will also need the relevant Qt version for that where initializer list support has been added to the QList constructor.

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

You could use the following code:

QList<int> list = QList<int>() << 1 << 1; 

or initializer list with C++11:

QList<int> list({1, 1}); 

You can enable the latter with the -std=c++0x or -std=c++11 option for gcc. You will also need the relevant Qt version for that where initializer list support has been added to the QList constructor.

like image 182
lpapp Avatar answered Sep 21 '22 19:09

lpapp