Results 1 to 5 of 5

Thread: QListWidget: double click item -> multiple itemDoubleClicked signals

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QListWidget: double click item -> multiple itemDoubleClicked signals

    Hi, I've a strange problem with a QListWidget; have defined this connection:

    Qt Code:
    1. connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doSomething(QListWidgetItem*)));
    To copy to clipboard, switch view to plain text mode 

    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?
    Giuseppe CalÃ

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QListWidget: double click item -> multiple itemDoubleClicked signals

    Do you connect the signal multiple times to the slot? For clarifying use Qt::UniqueConnection. Can you make an example?

  3. The following user says thank you to Lykurg for this useful post:

    jiveaxe (22nd March 2012)

  4. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidget: double click item -> multiple itemDoubleClicked signals

    Here a sample:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QDir>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. QDir shortcutsFolder(QDir::homePath() + QDir::separator() + m_currentDir);
    13. QFileInfoList shortcuts(shortcutsFolder.entryInfoList(QDir::Files, QDir::Name | QDir::IgnoreCase));
    14.  
    15. foreach (QFileInfo fi, shortcuts){
    16. QListWidgetItem *item = new QListWidgetItem(fi.fileName());
    17. connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doSomething(QListWidgetItem*)));
    18. ui->listWidget->addItem(item);
    19. }
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void MainWindow::doSomething(QListWidgetItem *item)
    28. {
    29. qDebug() << item->text();
    30. }
    To copy to clipboard, switch view to plain text mode 

    Adding Qt::UniqueConnection to connect line fixes the behaviour.
    Giuseppe CalÃ

  5. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListWidget: double click item -> multiple itemDoubleClicked signals

    move
    Qt Code:
    1. connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doSomething(QListWidgetItem*)));
    To copy to clipboard, switch view to plain text mode 
    from foreach() loop.
    You're not connecting "item" in there so you need to call it only once.

  6. The following user says thank you to Spitfire for this useful post:

    jiveaxe (22nd March 2012)

  7. #5
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidget: double click item -> multiple itemDoubleClicked signals

    You're absolutely right. Thanks
    Giuseppe CalÃ

Similar Threads

  1. Replies: 2
    Last Post: 24th June 2009, 15:38
  2. Replies: 6
    Last Post: 5th June 2009, 09:38
  3. Replies: 2
    Last Post: 11th January 2009, 23:24
  4. Multiple Lines per item in a QListWidget/QListView
    By youkai in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2008, 21:44
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.