QListWidget:项目点击事件 [英] QListWidget : Event on item click

查看:142
本文介绍了QListWidget:项目点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有以下内容:

Basically, what I have is the following :

QListWidget,其中包含一些项目,如下所示:

A QListWidget, with some items in it like this :

ListMail 是我的QListWidget.在此QListWidget中,我具有以下元素:邮件1",邮件2",...

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

我不知道如何在(例如)"Mail 1"绑定到插槽(onClick)或类似的东西上发出信号.

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.

我已经尝试过类似的事情: connect(ui-> listMail-> selectedItems(0),SIGNAL(triggered()),this,SLOT(openMessage()),但根本不起作用...

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

有帮助吗?

谢谢!

推荐答案

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

You must bind to the itemClicked signal. 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:

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

这篇关于QListWidget:项目点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆