Question

In my qt app I have this object, filled before setting up my QTreeWidget's content:

QList<QTreeWidgetItem*> items;

I fill the QList by this way:

QVariant qv; // I need this for "attaching" to the item my linuxPackage object
qv.setValue(linuxPackage);
packRow->setData(1, Qt::UserRole,qv); // packRow is my own object inherited from QTreeWidgetItem, I "put" the QVariant into it
items.append(packRow); // then I put my item into the QList

at the end of the work, my QList has almost 1000 items.

I need to iterate over them and for each item I need to get the "linuxPackage" data by this (tested and working) way:

Pkg linuxPackage = this->data(1,Qt::UserRole).value<Pkg>(); // Pkg is my own class for the linuxPackage object

So, I've been trying to extract needed data by this manner:

QList<QTreeWidgetItem*>::iterator iter; 
for (iter = items.begin(); iter != items.end(); ++iter){
    Pkg pack = iter->data(1,Qt::UserRole).value<Pkg>();
}

But nothing works, I can not even get the program compiling. Help! :D

Was it helpful?

Solution

Perhaps:

(*iter)->data(1,Qt::UserRole).value<Pkg>();

BTW, an easier way of doing this with Qt4:

foreach (const QTreeWidgetItem *item, items) {
    Pkg pack = item->data(1,Qt::UserRole).value<Pkg>();
}

at the very least, you should use const_iterators =)

QList<QTreeWidgetItem*>::const_iterator iter; 
for (iter = items.constBegin(); iter != items.constEnd(); ++iter){
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top