Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QListWidget : Event on item click

Tags:

c++

qt

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 !

like image 542
Yanis Boucherit Avatar asked Mar 09 '13 13:03

Yanis Boucherit


Video Answer


2 Answers

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.
    }
}
like image 99
andref Avatar answered Oct 09 '22 01:10

andref


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.

like image 23
Anthony Avatar answered Oct 09 '22 00:10

Anthony