PDA

View Full Version : for loop and QList<QAction*>



ravas
12th October 2015, 08:31
Given a QList<QAction*>
do you recommend writing a for loop as:
for(auto const& item: list)

anda_skoa
12th October 2015, 09:16
You can do that.

If "list" is not const this will call non const versions of begin() and end(), causing the QList to detach if the reference count is greater than 1, i.e. create a "deep copy".

Cheers,
_

ravas
12th October 2015, 09:33
Thanks. How would you write it if the list is not const?

anda_skoa
12th October 2015, 10:51
You either make it const


const QList<QAction*> constList = list;
for (.... : constList)

or


for (.... : const_cast<const QList<QAction*>& >(list))

or you use Qt's foreach


foreach (const QAction* action, list)


Cheers,
_