Results 1 to 8 of 8

Thread: How to add an QObject to QListWidget

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to add an QObject to QListWidget

    Hi,

    Is it possible to add an object to a list widget item? In MFC I generally attach objects to items in a list, example ( this is from MFC ):

    Qt Code:
    1. m_MessageList.SetItemDataPtr( nIndex, (void*)pMess );
    To copy to clipboard, switch view to plain text mode 

    Where pMess is of type CMessage ( own class ) and m_MessageList is of CListBox.

    Best regards,
    Steve

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to add an QObject to QListWidget

    You can store anything a QVariant can hold as user data:
    Qt Code:
    1. // QVariant data;
    2. // QListWidgetItem* item;
    3. item->setData(Qt::UserRole, data);
    To copy to clipboard, switch view to plain text mode 

    See Q_DECLARE_METATYPE() in case built-in datatypes supported by QVariant are not enough.
    J-P Nurmi

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add an QObject to QListWidget

    Hi,

    Thanks, that is exactly what I just did

    Now, having trouble getting the item data back.

    Qt Code:
    1. CDADcb::CMessage* pMess = ui.messageList->item(i)->data(Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 

    Keeps saying cannot convert to CDADcb::CMessage from QVariant, I've tried casting this but to no avail.

    Kind regards,
    Steve

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to add an QObject to QListWidget

    Try:
    Qt Code:
    1. CDADcb::CMessage* pMess = ui.messageList->item(i)->data(Qt::UserRole).value<CDADcb::CMessage*>();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add an QObject to QListWidget

    Thanks JPN, you're a star

    BTW, did have to do the following in the class header :

    Qt Code:
    1. Q_DECLARE_METATYPE(CDADcb::CMessage*)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add an QObject to QListWidget

    Problem now is, pMess is always NULL?

    This is how I set up the list box :

    Qt Code:
    1. CDADcb::CMessage* pMess = (CDADcb::CMessage*)i.value();
    2. if( pMess )
    3. {
    4. newItem->setFlags( newItem->flags() | Qt::ItemIsUserCheckable );
    5. newItem->setCheckState(Qt::Unchecked);
    6. newItem->setText( pMess->name );
    7. newItem->setData( Qt::UserRole, pMess );
    8. ui.messageList->insertItem( ui.messageList->count(), newItem);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Dunno what is wrong???

    Regards,
    Steve

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to add an QObject to QListWidget

    Qt Code:
    1. newItem->setData( Qt::UserRole, QVariant::fromValue(pMess) );
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    steg90 (14th May 2007)

  9. #8
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add an QObject to QListWidget

    Once again, many thanks for all your great help, now works a treat

    Kind regards,
    Steve

Similar Threads

  1. Replies: 13
    Last Post: 15th December 2006, 11:52

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.