如何在 QListWidget 中设置所选项目?

发布于 2024-11-14 04:18:01 字数 215 浏览 2 评论 0原文

我使用下面的代码将两个项目添加到列表小部件中。现在我想将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小兔几 2024-11-21 04:18:01

您可以这样做:

QStringList items;
items << "All" << "Weekend Plus" ;
listWidgetTimeSet->addItems(items);
listWidgetTimeSet->setCurrentRow( 1 );

但这意味着您知道“周末加号位于第二行,您需要记住这一点,以防万一您有其他项目。

或者您这样做:

QListWidgetItem* all_item = new QListWidgetItem( "All" );
QListWidgetItem* wp_item = new QListWidgetItem( "Weekend Plus" );
listWidgetTimeSet->addItem( all_item );
listWidgetTimeSet->addItem( wp_item );
listWidgetTimeSet->setCurrentItem( wp_item );

希望有帮助。

编辑:

根据您的评论,我建议使用项目视图的编辑触发器,它允许您通过键入要添加的内容并按回车键或回车键来直接添加项目您刚刚添加的项目是。 ,现在显示为 QListWidget 中的一个项目。

listWidgetTimeSet->setEditTriggers( QAbstractItemView::DoubleClicked ); // example

已选择 信息,请参阅文档

如果您想输入新的 项目在其他地方,当然也有一种方法,假设您有一个行编辑,并使用您在其中输入的名称添加项目,现在您希望已添加该项目的 ListWidget 更改为该新项目。假定新项目位于最后一个位置(因为它是最后添加的),您可以将当前行更改为最后一行。 (请注意,count() 还会计算隐藏项目(如果有的话)

listWidgetTimeSet->setCurrentRow( listWidgetTimeSet->count() - 1 ); // size - 1 = last item

You could either do it like this:

QStringList items;
items << "All" << "Weekend Plus" ;
listWidgetTimeSet->addItems(items);
listWidgetTimeSet->setCurrentRow( 1 );

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:

QListWidgetItem* all_item = new QListWidgetItem( "All" );
QListWidgetItem* wp_item = new QListWidgetItem( "Weekend Plus" );
listWidgetTimeSet->addItem( all_item );
listWidgetTimeSet->addItem( wp_item );
listWidgetTimeSet->setCurrentItem( wp_item );

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.

listWidgetTimeSet->setEditTriggers( QAbstractItemView::DoubleClicked ); // example

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)

listWidgetTimeSet->setCurrentRow( listWidgetTimeSet->count() - 1 ); // size - 1 = last item
戏舞 2024-11-21 04:18:01

也许

    ui->listWidgetTimeSet->item(1)->setSelected(true);

也尝试一下

    ui->listWidgetTimeSet->setCurrentRow(1);

Maybe

    ui->listWidgetTimeSet->item(1)->setSelected(true);

Try also

    ui->listWidgetTimeSet->setCurrentRow(1);
dawn曙光 2024-11-21 04:18:01

要使用原始文本设置当前 QList raw:

ui->List->setCurrentItem(ui->List->findItems("Raw x Content",Qt::MatchExactly)[0]);

To set current QList raw with the raw text :

ui->List->setCurrentItem(ui->List->findItems("Raw x Content",Qt::MatchExactly)[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文