Results 1 to 5 of 5

Thread: QList::indexOf() ?

  1. #1
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question QList::indexOf() ?

    Hello,

    to find the index of a Widget from a QList, i do this:
    Qt Code:
    1. QList<QPair<MyWidget*, int> > list
    2.  
    3. int i
    4. for(i=0;i<list.size();i++){
    5. if( list[i].first == mywidget )
    6. break;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Is it possible to use indexOf() like this?

    Qt Code:
    1. list.indexOf(T.first)
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList::indexOf() ?

    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:

    Qt Code:
    1. QHash<QWidget*, int> hash;
    2. //...
    3. QWidget *mywidget;
    4. int value = hash.value(mywidget);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    cyberduck (23rd March 2012)

  4. #3
    Join Date
    Nov 2010
    Location
    iran/isfahan
    Posts
    13
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QList::indexOf() ?

    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)

  5. The following user says thank you to alireza.mirian for this useful post:

    cyberduck (23rd March 2012)

  6. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList::indexOf() ?

    You could also use QMap.

  7. The following user says thank you to Spitfire for this useful post:

    cyberduck (23rd March 2012)

  8. #5
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: QList::indexOf() ?

    Thanks, QHash and QMap is much better to do that!

Similar Threads

  1. QList<A*> indexOf
    By Moppel in forum General Programming
    Replies: 6
    Last Post: 8th August 2017, 14:17
  2. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  3. What is faster, QRegExp, or QByteArray::indexOf
    By _Stefan in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2010, 18:03
  4. Replies: 4
    Last Post: 20th August 2010, 13:54
  5. QString::indexOf Qt::CaseInsensitive
    By Carlsberg in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2009, 18:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.