Results 1 to 9 of 9

Thread: Out of multiple QTableWidgets how to find which one is selected

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Out of multiple QTableWidgets how to find which one is selected

    I'm using 4 QTableWidget s to display data. On clicking any of the first three QTableWidget s, I want to display all columns data of the selected row of that particular QTableWidget in the fourth one.

    I'm able to create signals & a slot to get the clicked row & column, but how do I know the corresponding QTableWidget ?

    Qt Code:
    1. connect(table1, SIGNAL(cellClicked(int,int)), this, SLOT(detailedDisplay(int,int)));
    2. connect(table2, SIGNAL(cellClicked(int,int)), this, SLOT(detailedDisplay(int,int)));
    3. connect(table3, SIGNAL(cellClicked(int,int)), this, SLOT(detailedDisplay(int,int)));
    To copy to clipboard, switch view to plain text mode 

    Kindly help me, thank you.

  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: Out of multiple QTableWidgets how to find which one is selected

    There are couple of ways,

    1. Use the sender() to determine the signal sender, cast it to QTableWidget, but be carefule it not always safe, unless you handle it properly.
    2. Use a derived QTableWidget to emit a custom signal to include the TableWidget pointer as a signal parameter along with row and col.
    3. Use a signal mapper along side of the existing signals and pass the TableWidget QObject pointer as signal mapper signal, and then the required signal.
    4. Send a data or global data identifier as a signal parameter, instead of the QTableWidget specific row/col, that way you woun't need to bother which TableWidget has sent the signal.

    I recommend the 4th way, but it may deppend on the specific requirement.
    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.

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

    rawfool (26th April 2013)

  4. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Out of multiple QTableWidgets how to find which one is selected

    Hi,
    Thank you for the reply. I tried your 2nd suggestion.
    2. Use a derived QTableWidget to emit a custom signal to include the TableWidget pointer as a signal parameter along with row and col.
    I created a derived class CTableWidget and created a signal & slot connection. But slot is not getting called. I didn't understand why

    Qt Code:
    1. CTableWidget::CTableWidget(QWidget *parent, int nRows, int nCols) :
    2. QTableWidget(parent)
    3. {
    4. this->setRowCount(nRows);
    5. this->setColumnCount(nCols);
    6. connect(this, SIGNAL(cellActivated(int,int)), this, SLOT(getRowCol(int,int)));
    7. qDebug("Inside Constructor");
    8. }
    9.  
    10.  
    11. void CTableWidget::getRowCol(int rowID, int columnID) // This is a slot
    12. {
    13. qDebug("Inside getRowCol() slot"); // This debug msg isn't getting printed
    14. QString objName = this->objectName();
    15. emit sigObjRowCol(objName, rowID, columnID);
    16. }
    To copy to clipboard, switch view to plain text mode 
    To find out which QTableWidget is clicked, I'm assigning a object name & using string compare.

    And in the class where I created objects of my custom widget (CTableWidget), I'm doing this
    Qt Code:
    1. vulnReports = new CTableWidget(0, 20, 3);
    2. vulnReports->setObjectName("vulnReports");
    3.  
    4. complReports = new CTableWidget(0, 20, 3);
    5. complReports->setObjectName("complReports");
    6.  
    7. patchReports = new CTableWidget(0, 10, 3);
    8. patchReports->setObjectName("patchReports");
    9.  
    10. connect(vulnReports, SIGNAL(sigObjRowCol(QString,int,int)), this, SLOT(remediateIndiv(QString,int,int)));
    11. connect(complReports, SIGNAL(sigObjRowCol(QString,int,int)), this, SLOT(remediateIndiv(QString,int,int)));
    12. connect(patchReports, SIGNAL(sigObjRowCol(QString,int,int)), this, SLOT(remediateIndiv(QString,int,int)));
    To copy to clipboard, switch view to plain text mode 
    Am I missing something ?

  5. #4
    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: Out of multiple QTableWidgets how to find which one is selected

    Am I missing something ?
    Yes, the question?
    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.

  6. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Out of multiple QTableWidgets how to find which one is selected

    I created custom TableWidget and custom signal. The custom signal emits the row, column numbers and object name.
    First on cellActivated(int,int) signal I'm invkoing getRowCol(int, int) slot and in that I'm getting object name & emitting another signal with row, col, objName. All this in CTableWidget class (custom QTableWidget) and created objects of this.
    But the slot getRowCol() is not invoked on click of any cell and I've checked this with qDebug() message also. Any reason ?
    Thank you.
    Last edited by rawfool; 27th April 2013 at 09:51.

  7. #6
    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: Out of multiple QTableWidgets how to find which one is selected

    1. Make sure class definition has Q_OBJECT macro
    2. Is there any message in the application output?
    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.

  8. #7
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Out of multiple QTableWidgets how to find which one is selected

    Yes Q_OBJECT macro is added by default.

    2. Is there any message in the application output?
    Application runs quite normal. No spl message in output on running the application.

    Thank you very much for your help.

  9. #8
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Out of multiple QTableWidgets how to find which one is selected

    once try with cellClicked ( int row, int column ) signal ... and check ...

  10. The following user says thank you to pradeepreddyg95 for this useful post:

    rawfool (29th April 2013)

  11. #9
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Out of multiple QTableWidgets how to find which one is selected

    Oh! that was the problem. I used cellActivated(int,int)) instead of cellClicked(int,int).
    I read the documentation, but didn't understand between the two. What is the difference between cellActivated(int,int)) and cellClicked(int,int) ?

    Thank you Pradeep and Santosh.

Similar Threads

  1. Replies: 4
    Last Post: 31st January 2013, 15:40
  2. Replies: 4
    Last Post: 6th March 2011, 09:54
  3. Replies: 3
    Last Post: 6th October 2010, 08:33
  4. Replies: 2
    Last Post: 30th July 2009, 10:20
  5. undo for multiple selected items
    By mooreaa in forum Qt Programming
    Replies: 3
    Last Post: 13th July 2008, 14:13

Tags for this Thread

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.