Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

c++ - QListWidget : Event on item click

Basically, what I have is the following :

A QListWidget, with some items in it like this :

ListMail is my QListWidget. In this QListWidget, I have elements like : "Mail 1", "Mail 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.

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

Any help ?

Thanks !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...