如何在 QListWidget 中设置所选项目?
我使用下面的代码将两个项目添加到列表小部件中。现在我想将“Weekend Plus”设置为列表小部件中的选定项目,我该怎么做?
QStringList items;
items << "All" << "Weekend Plus" ;
ui->listWidgetTimeSet->addItems(items);
I am adding two items to a listwidget using the code below. Now I want to set "Weekend Plus" as selected item in the listwidget, how do I do that?
QStringList items;
items << "All" << "Weekend Plus" ;
ui->listWidgetTimeSet->addItems(items);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做:
但这意味着您知道“周末加号位于第二行,您需要记住这一点,以防万一您有其他项目。
或者您这样做:
希望有帮助。
编辑:
根据您的评论,我建议使用项目视图的编辑触发器,它允许您通过键入要添加的内容并按回车键或回车键来直接添加项目您刚刚添加的项目是。 ,现在显示为 QListWidget 中的一个项目。
已选择 信息,请参阅文档。
如果您想输入新的 项目在其他地方,当然也有一种方法,假设您有一个行编辑,并使用您在其中输入的名称添加项目,现在您希望已添加该项目的 ListWidget 更改为该新项目。假定新项目位于最后一个位置(因为它是最后添加的),您可以将当前行更改为最后一行。 (请注意,
count()
还会计算隐藏项目(如果有的话)You could either do it like this:
But that would mean that you know that "Weekend Plus is on second row and you need to remember that, in case you other items.
Or you do it like that:
Hope that helps.
EDIT:
According to your comment, I suggest using the edit triggers for item views. It allows you to add items directly by just typing what you want to add and press the return or enter key. The item you just added is selected and now appears as an item in the QListWidget.
See the docs for more information.
If you want to enter your new item somewhere else, there is a way of course, too. Let's say you have a line edit and you add the item with the name you entered there. Now you want the ListWidget where the item has been added to change to that new item. Assumed the new item is on the last position (because it has been added last) you can change the current row to the last row. (Note that
count()
also counts hidden items if you have any)也许
也尝试一下
Maybe
Try also
要使用原始文本设置当前 QList raw:
To set current QList raw with the raw text :