Results 1 to 4 of 4

Thread: Associate userData to items added to a QListWidget.

  1. #1
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Associate userData to items added to a QListWidget.

    Hi,

    I have a Dialog implementing 2 QListWidgets.
    The lists are filled before the dialog is executed, and in order to make it convivial for the user I am setting the lists whith strings that are user friendly f.e.:
    0x1234/4660 - Engine 1 speed indication
    0x1333/4915 - Engine 1 temperature
    ...
    ...
    The user can then drag and drop between the 2 lists.
    When the widget is closed I would like to get what is in both lists.

    I am wondering what would be the best way to associate userData to each item placed in the lists (somehow like what is possible with QComboBox::addItem(const QString & text, const QVariant & userData = QVariant()).

    In such a case, the post treatment would be easier for me. I would then just itterate through the lists in order to get the userDatas composing each list instead of the user friendly string.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Associate userData to items added to a QListWidget.

    did you try to use QListWidgetItem::setData?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    schall_l (4th August 2009)

  4. #3
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Associate userData to items added to a QListWidget.

    Thank you, I guess this could work for me.
    I'll post some sample code later.

  5. #4
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Associate userData to items added to a QListWidget.

    This is the code that worked for me thanks to the reply from Spirit.

    The displayed text in the list widget is the one passed via QString description, while the 'internal' representation of the data is the one passed via unsigned int id. The 'internal' representation is stored via setData using the UserRole and is later on retrieved via the data function.

    Qt Code:
    1. void FilterDialog::addIdentifer(QString description, unsigned int id) {
    2. QListWidgetItem *item = new QListWidgetItem(description, listA);
    3. item->setData(Qt::UserRole, id);
    4. listA->addItem(item);
    5. }
    6.  
    7. QList<unsigned int> FilterDialog::getIdentifiers() {
    8. QList<unsigned int> tmp;
    9. for (int i=0; i<listA->count(); i++) {
    10. tmp.append( listA->item(i)->data(Qt::UserRole).toUInt() );
    11. }
    12. return tmp;
    13. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to make QListWidget items editable?
    By montylee in forum Qt Programming
    Replies: 14
    Last Post: 3rd October 2008, 09:57
  2. Replies: 1
    Last Post: 5th September 2008, 23:54
  3. Get Visible Items from the QListWidget
    By srj in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 20:13
  4. Qt 4.2: QListWidget changes size of its items
    By KingFish in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 11:06
  5. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 20:04

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.