Results 1 to 2 of 2

Thread: QTableWidget cellClicked and cellDoubleClicked

  1. #1

    Default QTableWidget cellClicked and cellDoubleClicked

    I have a QTableWidget with row selection, and i'm trying to treat 3 signals:
    •cellClicked
    •cellDoubleClicked
    •customContextMenuRequested

    Things seem to be fine in code: i had in mind the connect syntax, type params are correct and match, etc; and to be more specific, i "know" that the code is correct because i have the following situation:
    •if i connect the 3 signals to their respective slots, only the single click and the context menú work.
    •if i connect just one signal each time i compile the code and i run the program, that signal is working well (for the 3 of them).
    •if i connect the single click signal and the context menu, commenting the connect macro for the double click, they work well. Same for double click and context menu.

    BUT if i connect the single click and the double click, the double click is not being treated by my custom slot.

    Just to clarify, each signal has a different slot, and as i meantioned above, they work well if i just connect one of them and comment the other 2 in code.

    So my question is: is there any bug with the cellClicked and cellDoubleClick working simultaneously? do i have to set some flag, attribute or whatever that belongs to the QTableWidget?

    I'm running out of ideas, thanks for the help!

    And also, maybe the code should help:

    table and slots declaration:

    Qt Code:
    1. QTableWidget * table;
    2.  
    3. public slots:
    4. void tableChange(int row, int column);
    5. void tableChangeDbl(int row, int column);
    6. void PopupMenuTableShow(const QPoint &);
    To copy to clipboard, switch view to plain text mode 

    the connects:

    Qt Code:
    1. connect(table, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(tableChangeDbl(int, int)));
    2. connect(table, SIGNAL(cellClicked(int, int)), this, SLOT(tableChange(int, int)));
    3. connect(table, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(PopupMenuTableShow(const QPoint &)));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableWidget cellClicked and cellDoubleClicked

    Here is an example code where all three signals are emitted.

    Make a note of this

    1. When mouse left cliecked -> cellClicked() is emitted.
    2. When mouse left double clicked -> cellClicked() and cellDoubleClicked() is emitted.
    3. When mouse right clicked -> cellClicked() and customContextMenuRequested() is emitted.

    QTableWidget.jpg
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. class Widget : public QTextBrowser
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit Widget(QTableWidget * table)
    10. {
    11. connect(table, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(tableChangeDbl(int, int)));
    12. connect(table, SIGNAL(cellClicked(int, int)), this, SLOT(tableChange(int, int)));
    13.  
    14. table->setContextMenuPolicy(Qt::CustomContextMenu);
    15. connect(table, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(PopupMenuTableShow(const QPoint &)));
    16. show();
    17. }
    18.  
    19. public slots:
    20. void tableChange(int row, int column)
    21. {
    22. append(QString("Clicked %1, %2").arg(row).arg(column));
    23. }
    24.  
    25. void tableChangeDbl(int row, int column)
    26. {
    27. append(QString("Double Clicked %1, %2").arg(row).arg(column));
    28. }
    29.  
    30. void PopupMenuTableShow(const QPoint & point)
    31. {
    32. append(QString("Menu %1, %2").arg(point.x()).arg(point.y()));
    33. }
    34. };
    35.  
    36. int main(int argc, char **argv)
    37. {
    38. QApplication app(argc, argv);
    39.  
    40. QTableWidget tableWidget;
    41. Widget widget(&tableWidget);
    42.  
    43. tableWidget.setRowCount(10);
    44. tableWidget.setColumnCount(2);
    45. for(int r = 0 ; r < tableWidget.rowCount(); r++)
    46. for(int c = 0 ; c < tableWidget.columnCount(); c++)
    47. tableWidget.setItem(r, c, new QTableWidgetItem(QString("%1:%2").arg(r).arg(c)));
    48.  
    49. tableWidget.setWindowTitle("QTableWidget");
    50. widget.setWindowTitle("Widget");
    51.  
    52. tableWidget.show();
    53. widget.show();
    54.  
    55. return app.exec();
    56. }
    57.  
    58. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Qtablewidget example
    By thefatladysingsopera in forum Newbie
    Replies: 2
    Last Post: 29th July 2011, 09:27
  2. Replies: 1
    Last Post: 6th January 2011, 04:19
  3. Replies: 2
    Last Post: 1st August 2010, 09:55
  4. QTableWidget
    By dragon in forum Qt Programming
    Replies: 14
    Last Post: 18th April 2007, 19:15
  5. QTableWidget
    By chak_med in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2006, 15:53

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.