简体   繁体   中英

Problem with QVariant/QTreeWidgetItem/iterator on qt4.4.3

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

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){
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM