Results 1 to 7 of 7

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

  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.

  5. #5
    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)

    No, it's not derived...
    here is a little bit of code:
    Qt Code:
    1. Custom_Widget::Custom_Widget(QString name_par, QString date_par, QString about_par, QString picture_par, QWidget *parent)
    2. : QWidget(parent)
    3.  
    4. {
    5. imageLabel = new QLabel;
    6. name = new QLabel(name_par);
    To copy to clipboard, switch view to plain text mode 
    ...

    And some code i use to add custom widgets into my list Widget:
    Qt Code:
    1. for (int i = 0; i<new_ml.size(); i++)
    2. {
    3. listWidget->insertItem(i, "");
    4. listWidget->item(i)->setToolTip(new_ml[i]->name());
    5. listWidget->setItemWidget(listWidget->item(i), new Custom_Widget
    6. (new_ml[i]->name(), new_ml[i]->globalDate(), new_ml[i]->about(), new_ml[i]->picture()));
    7. listWidget->item(i)->setSizeHint(QSize (350,470));
    8. listWidget->setSpacing(1);
    9. }
    To copy to clipboard, switch view to plain text mode 


    Added after 13 minutes:


    Well.. i know why my previous cast wasn't working. I thought the (0) was the row on which the item was, but actually it wasn't. That's why i was getting wrong pointer, that is why my app was crashing... But I still have no idea how to use this cast properly...
    Last edited by Archa4; 7th April 2011 at 13:00.

  6. #6
    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)

    ItemWidget(...) is the answer in this case.

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

    Archa4 (7th April 2011)

  8. #7
    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 worked it out...
    Had to use:
    Qt Code:
    1. Custom_Widget *widget = qobject_cast<Custom_Widget *>(listWidget->itemWidget(listWidget->item(0)));
    To copy to clipboard, switch view to plain text mode 

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.