PDA

View Full Version : QListWidget: double click item -> multiple itemDoubleClicked signals



jiveaxe
22nd March 2012, 15:59
Hi, I've a strange problem with a QListWidget; have defined this connection:


connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doSomething(QListWidgetItem*)));

doSomething() contains a simple qDebug(), just for testing. Now, when I double click an item, in the application output I got multiple time the same qDebug() message.

I'm doing anything wrong?

Lykurg
22nd March 2012, 16:05
Do you connect the signal multiple times to the slot? For clarifying use Qt::UniqueConnection. Can you make an example?

jiveaxe
22nd March 2012, 16:28
Here a sample:


#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDir>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QDir shortcutsFolder(QDir::homePath() + QDir::separator() + m_currentDir);
QFileInfoList shortcuts(shortcutsFolder.entryInfoList(QDir::File s, QDir::Name | QDir::IgnoreCase));

foreach (QFileInfo fi, shortcuts){
QListWidgetItem *item = new QListWidgetItem(fi.fileName());
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doSomething(QListWidgetItem*)));
ui->listWidget->addItem(item);
}
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::doSomething(QListWidgetItem *item)
{
qDebug() << item->text();
}


Adding Qt::UniqueConnection to connect line fixes the behaviour.

Spitfire
22nd March 2012, 17:03
move
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doSomething(QListWidgetItem*)));from foreach() loop.
You're not connecting "item" in there so you need to call it only once.

jiveaxe
22nd March 2012, 17:07
You're absolutely right. Thanks