PDA

View Full Version : QListWidget accessing item info (listWidget->setItemWidget)



Archa4
7th April 2011, 12:00
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:

QWidget *widget = qobject_cast<QWidget *>(Custom_Widget);
But i think that is completely wrong...
Also i tried

QWidget *widget = static_cast<QListWidgetItem *>(listWidget->item(0));
But this is still wrong... Can someone help?

Zlatomir
7th April 2011, 12:20
See the QListWidgetItem (http://doc.qt.nokia.com/latest/qlistwidgetitem.html) documentation, it has the text() (http://doc.qt.nokia.com/latest/qlistwidgetitem.html#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 (http://doc.qt.nokia.com/4.7/model-view-programming.html)

Archa4
7th April 2011, 12:30
I cannot use text because i use Custom widget as my ListWidgetItem...

I tried this one:

Custom_Widget *widget = qobject_cast<Custom_Widget *>(listWidget->itemWidget(0));

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

Zlatomir
7th April 2011, 12:35
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.

Archa4
7th April 2011, 13:00
No, it's not derived...
here is a little bit of code:

Custom_Widget::Custom_Widget(QString name_par, QString date_par, QString about_par, QString picture_par, QWidget *parent)
: QWidget(parent)

{
imageLabel = new QLabel;
name = new QLabel(name_par);
...

And some code i use to add custom widgets into my list Widget:

for (int i = 0; i<new_ml.size(); i++)
{
listWidget->insertItem(i, "");
listWidget->item(i)->setToolTip(new_ml[i]->name());
listWidget->setItemWidget(listWidget->item(i), new Custom_Widget
(new_ml[i]->name(), new_ml[i]->globalDate(), new_ml[i]->about(), new_ml[i]->picture()));
listWidget->item(i)->setSizeHint(QSize (350,470));
listWidget->setSpacing(1);
}

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...

Zlatomir
7th April 2011, 13:00
ItemWidget(...) (http://doc.qt.nokia.com/latest/qlistwidget.html#itemWidget) is the answer in this case.

Archa4
7th April 2011, 13:05
:)
I worked it out...
Had to use:

Custom_Widget *widget = qobject_cast<Custom_Widget *>(listWidget->itemWidget(listWidget->item(0)));