Results 1 to 7 of 7

Thread: QListWidget accessing item info (listWidget->setItemWidget)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget accessing item info (listWidget->setItemWidget)

    I want to get information from the item in the QListWidget. The item is set using setItemWidget.
    I got an answer, that i should use cast on widget and get info that way.
    I have no idea what did that mean, but i tried some things - nothing worked.
    My list item contains few labels with text and some pictures. I need to get the text that is in the labels.
    My last thought was:
    Qt Code:
    1. QWidget *widget = qobject_cast<QWidget *>(Custom_Widget);
    To copy to clipboard, switch view to plain text mode 
    But i think that is completely wrong...
    Also i tried
    Qt Code:
    1. QWidget *widget = static_cast<QListWidgetItem *>(listWidget->item(0));
    To copy to clipboard, switch view to plain text mode 
    But this is still wrong... Can someone help?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QListWidget accessing item info (listWidget->setItemWidget)

    See the QListWidgetItem documentation, it has the text() member function you might want to use.

    //i don't think that QListWidgetItem is in the same hierarchy with QWidget (so the casts won't work)

    And if this isn't enough maybe you need to inherit QListWidgetItem, or use the more flexible Model-View

  3. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget accessing item info (listWidget->setItemWidget)

    I cannot use text because i use Custom widget as my ListWidgetItem...

    I tried this one:
    Qt Code:
    1. Custom_Widget *widget = qobject_cast<Custom_Widget *>(listWidget->itemWidget(0));
    To copy to clipboard, switch view to plain text mode 

    But the application crashes....
    In theory should it work? And will it have the same information that itemWidget(0) has?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QListWidget accessing item info (listWidget->setItemWidget)

    And isn't Custom_Widget derived from ListWidgetItem?

    Maybe you need to show us more code so that we can see what you did there, or make a small compilable example in which you replicate the issue.

Similar Threads

  1. Replies: 2
    Last Post: 3rd February 2011, 10:07
  2. Replies: 0
    Last Post: 29th November 2010, 05:34
  3. Replies: 7
    Last Post: 19th October 2010, 08:32
  4. Replies: 0
    Last Post: 16th June 2010, 13:09
  5. Replies: 1
    Last Post: 5th September 2008, 23:54

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.