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 ?
Code:
connect(table1, SIGNAL(cellClicked(int,int)), this, SLOT(detailedDisplay(int,int)));
connect(table2, SIGNAL(cellClicked(int,int)), this, SLOT(detailedDisplay(int,int)));
connect(table3, SIGNAL(cellClicked(int,int)), this, SLOT(detailedDisplay(int,int)));
Kindly help me, thank you.
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.
Re: Out of multiple QTableWidgets how to find which one is selected
Hi,
Thank you for the reply. I tried your 2nd suggestion.
Quote:
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
Code:
CTableWidget
::CTableWidget(QWidget *parent,
int nRows,
int nCols
) :{
this->setRowCount(nRows);
this->setColumnCount(nCols);
connect(this, SIGNAL(cellActivated(int,int)), this, SLOT(getRowCol(int,int)));
qDebug("Inside Constructor");
}
void CTableWidget::getRowCol(int rowID, int columnID) // This is a slot
{
qDebug("Inside getRowCol() slot"); // This debug msg isn't getting printed
QString objName
= this
->objectName
();
emit sigObjRowCol(objName, rowID, columnID);
}
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
Code:
vulnReports = new CTableWidget(0, 20, 3);
vulnReports->setObjectName("vulnReports");
complReports = new CTableWidget(0, 20, 3);
complReports->setObjectName("complReports");
patchReports = new CTableWidget(0, 10, 3);
patchReports->setObjectName("patchReports");
connect(vulnReports,
SIGNAL(sigObjRowCol
(QString,
int,
int)),
this,
SLOT(remediateIndiv
(QString,
int,
int)));
connect(complReports,
SIGNAL(sigObjRowCol
(QString,
int,
int)),
this,
SLOT(remediateIndiv
(QString,
int,
int)));
connect(patchReports,
SIGNAL(sigObjRowCol
(QString,
int,
int)),
this,
SLOT(remediateIndiv
(QString,
int,
int)));
Am I missing something ?
Re: Out of multiple QTableWidgets how to find which one is selected
Quote:
Am I missing something ?
Yes, the question?
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.
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?
Re: Out of multiple QTableWidgets how to find which one is selected
Yes Q_OBJECT macro is added by default.
Quote:
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.
Re: Out of multiple QTableWidgets how to find which one is selected
once try with cellClicked ( int row, int column ) signal ... and check ...
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.