PDA

View Full Version : QList::indexOf() ?



cyberduck
18th March 2012, 13:19
Hello,

to find the index of a Widget from a QList, i do this:


QList<QPair<MyWidget*, int> > list

int i
for(i=0;i<list.size();i++){
if( list[i].first == mywidget )
break;
}


Is it possible to use indexOf() like this?


list.indexOf(T.first)

Thanks in advance

wysota
18th March 2012, 13:29
No, you can't do it this way. But you can improve (as in 'speed up') your search by using a hash instead of a list:


QHash<QWidget*, int> hash;
//...
QWidget *mywidget;
int value = hash.value(mywidget);

alireza.mirian
18th March 2012, 13:31
list.indexOf() accepts a argument of your list type, which is QPair in your example. and if a QPair in list is '==' with the passed Qpair, it will return it's index.
T seems to be undefined according to your code and "list.indexOf(T.first);" contains error because of that. (if you mean the Template type of the list, it's quite wrong) :)

Spitfire
20th March 2012, 15:21
You could also use QMap.

cyberduck
23rd March 2012, 06:52
Thanks, QHash and QMap is much better to do that!