C++ QListWidget : 项目点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15311241/
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 19:15:10  来源:igfitidea点击:

QListWidget : Event on item click

c++qt

提问by Yanis Boucherit

Basically, what I have is the following :

基本上,我所拥有的是以下内容:

A QListWidget, with some items in it like this :

一个 QListWidget,其中有一些项目,如下所示:

ListMailis my QListWidget. In this QListWidget, I have elements like : "Mail 1", "Mail 2", ...

ListMail是我的 QListWidget。在这个 QListWidget 中,我有这样的元素:“邮件 1”、“邮件 2”、...

And I don't have any idea, how can I make a signal on (for example) "Mail 1" bind to a slot(onClick) or something like that.

而且我不知道如何在(例如)“邮件 1”绑定到插槽(onClick)或类似的东西上发出信号。

I already tried things like : connect(ui->listMail->selectedItems(0), SIGNAL(triggered()), this, SLOT(openMessage()), but it doesn't work at all...

我已经尝试过诸如 : 之类的东西 connect(ui->listMail->selectedItems(0), SIGNAL(triggered()), this, SLOT(openMessage()),但它根本不起作用......

Any help ?

有什么帮助吗?

Thanks !

谢谢 !

回答by andref

You must bind to the itemClickedsignal. The signal will provide you with a QListWidgetItem*which is the item that was clicked. You can then examine it and check if it is the first one:

您必须绑定到itemClicked信号。该信号将为您提供QListWidgetItem*被点击的项目。然后您可以检查它并检查它是否是第一个:

MyClass::MyClass(QWidget* parent)
    : QWidget(parent)
{
    connect(ui->listMail, SIGNAL(itemClicked(QListWidgetItem*)), 
            this, SLOT(onListMailItemClicked(QListWidgetItem*)));
}

void MyClass::onListMailItemClicked(QListWidgetItem* item)
{
    if (ui->listMail->item(0) == item) {
        // This is the first item.
    }
}

回答by Anthony

QListWidget has a signal QListWidget::itemPressed(QListWidgetItem *item)that will tell you which item was clicked. You can connect this signal to your own slot. There are also other related signals. See the documentation.

QListWidget 有一个信号QListWidget::itemPressed(QListWidgetItem *item)会告诉你哪个项目被点击。您可以将此信号连接到您自己的插槽。还有其他相关信号。请参阅文档