PDA

View Full Version : Some Questions about QListWidget



Hossein
1st September 2012, 04:32
In CSharp its as simple as writting :

listBox1.Items.Add("Hello");
listBox1.Items.Add("There");

foreach (string item in listBox1.Items )
{
MessageBox.Show(item.ToString());
}

and i can easily add different objects to a list box and then retrieve them using foreach. I tried the same approach in Qt 4.8.2 but it seems they are different.though they look very similar at first.I found that Qt supports foreach so i went on and tried something like :

foreach(QListWidgetItem& item,ui->listWidget->items())
{
item.setTextColor(QColor::blue());
}

which failed clearly.It says the items() needs a parameter which confuses me.I am trying to iterate through the ListBox itself, so what does this mean? I tried passing the ListBox object as the parameter itself this again failed too:

foreach(QListWidgetItem& item,ui->listWidget->items(ui->listWidget))
{
item.setTextColor(QColor::blue());
}

So here are my questions:


How can I iterate through a QListWidget items in Qt?
Can i store objects as items in QListWidgets like C#?
How can i convert an object in QListWidgets to string(C#s ToString counter part in Qt) ?



(suppose i want to use a QMessagBox instead of that setTextColor and want to print out all string items in the QlistWidget.)

ChrisW67
1st September 2012, 06:57
Warning... code off top of head:


for (int i = 0; i < listWidget->count(); ++i) {
listWidget->item(i)->setForeground(Qt::yellow);

qDebug() << listWidget->item(i)->text();
}


I am not sure the second part of your question means. The QListWidget contains a list of pointers to QListWidgetItem objects. You can subclass QListWidgetItem to do something special and insert pointers to subclass objects. You can also store any data that can fit into a QVariant against a Qt::UserRole using QListWidgetItem::setData() and QListWidgetItem::::data().

Hossein
1st September 2012, 10:21
Warning... code off top of head:


for (int i = 0; i < listWidget->count(); ++i) {
listWidget->item(i)->setForeground(Qt::yellow);

qDebug() << listWidget->item(i)->text();
}


I am not sure the second part of your question means. The QListWidget contains a list of pointers to QListWidgetItem objects. You can subclass QListWidgetItem to do something special and insert pointers to subclass objects. You can also store any data that can fit into a QVariant against a Qt::UserRole using QListWidgetItem::setData() and QListWidgetItem::::data().
Thank you very much.
By my second question i meant, Is there a way that i can store refrences to any other objects rather than just simple string texts?
and you forgot to answer my third question on C#s Tostring() method counter part in QT.

tbscope
1st September 2012, 10:43
By my second question i meant, Is there a way that i can store refrences to any other objects rather than just simple string texts?
http://doc-snapshot.qt-project.org/5.0/qlistwidgetitem.html#setData


and you forgot to answer my third question on C#s Tostring() method counter part in QT.
http://doc-snapshot.qt-project.org/5.0/qvariant.html#toString
You can't turn an elephant into a giraffe. The object you want to "convert" to a string needs to be able to. It's ambiguous. This means that in most cases, you need to provide a function that does this. I'm not sure what C# ToString() does or is applicable to. Do you want to save the contents of a listview as a textfile? Then you need to write that function yourself.

ChrisW67
2nd September 2012, 10:05
Thank you very much.
By my second question i meant, Is there a way that i can store refrences to any other objects rather than just simple string texts?
and you forgot to answer my third question on C#s Tostring() method counter part in QT.

I answered all parts of your question... I did not 'forget' to answer anything.

My last paragraph gives you two different ways to extend what is stored as an item in the list widget: extend the QListWidgetItem or store arbitrary pieces of data in user roles against the item. You can store a pointer or other handle happily in a QVariant.

Line 4 of my example code shows how to do the equivalent of what I assume C#'s ToString() does: it gives you a string value of the specified list item (Qt::DisplayRole to be precise). The standard item can also have icon, colour, font, alignment, check state, and help information accessible through the data() function. If you use a user role to store custom information then you can use that to access any "toString()" functionality that might be associated with that custom information.