How can i iterate through QListWidget items and work with each item?

Posted by Hossein on Stack Overflow See other posts from Stack Overflow or by Hossein
Published on 2012-08-31T21:18:52Z Indexed on 2012/08/31 21:38 UTC
Read the original article Hit count: 254

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt