C++ 我如何遍历 QListWidget 项目并处理每个项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12222594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:02:46  来源:igfitidea点击:

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

c++qtqlistwidget

提问by Rika

In CSharp its as simple as writting :

在 CSharp 中,它就像编写一样简单:

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 the first. I found that Qt supports foreach so I went on and tried something like :

我可以轻松地将不同的对象添加到列表框中,然后使用 foreach 检索它们。我在 Qt 4.8.2 中尝试了相同的方法,但它们似乎不同。尽管它们一开始看起来非常相似。我发现 Qt 支持 foreach 所以我继续尝试类似的东西:

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:

这显然失败了。它说 items() 需要一个让我感到困惑的参数。我正在尝试遍历 ListBox 本身,这是什么意思?我尝试将 ListBox 对象作为参数本身传递,这也再次失败:

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) ?
  • 如何遍历 Qt 中的 QListWidget 项目?
  • 我可以像 C# 一样将对象存储为 QListWidgets 中的项目吗?
  • 如何将 QListWidgets 中的对象转换为字符串(Qt 中的 C#s ToString 计数器部分)?

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

(假设我想使用 QMessagBox 而不是 setTextColor 并且想打印出 QlistWidget 中的所有字符串项。)

回答by Xavier Holt

I don't think the items functiondoes what you think it does. It sounds like it's for decoding MIME data, not getting a list of all the items in the widget.

我不认为items 函数会做你认为的那样。这听起来像是用于解码 MIME 数据,而不是获取小部件中所有项目的列表。

I don't actually see any function to do exactly what you want, sadly. You could probably use findItemsas a workaround, but that seems ugly, if not downright abusive... At least you can still use the item functionwith good old forloops - they're not thatmuch more typing:

遗憾的是,我实际上没有看到任何功能可以完全按照您的要求执行。你也许可以使用findItems作为一种解决方法,但似乎难看,如果不是彻头彻尾的滥用......至少,你仍然可以使用产品功能与良好的老for循环-他们没有更多的打字:

for(int i = 0; i < listWidget->count(); ++i)
{
    QListWidgetItem* item = listWidget->item(i);
    //Do stuff!
}

Hope that helps!

希望有帮助!

回答by Thierry Joel

You can do something like this:

你可以这样做:

for(int i = 0; i < listWidget->count(); ++i)
{
    QString str = listwidget.item(i)->text();
    //Do stuff!
}

回答by Thierry Joel

Try making a pointer to each of the items if you are making the list items in code. But, if you are using the .ui file to make a list item try right clicking it and hitting edit stylesheet. You can edit it very easily that way.

如果您在代码中创建列表项,请尝试创建指向每个项的指针。但是,如果您使用 .ui 文件制作列表项,请尝试右键单击它并点击编辑样式表。您可以通过这种方式非常轻松地对其进行编辑。