PDA

View Full Version : Out of multiple QTableWidgets how to find which one is selected



rawfool
26th April 2013, 07:42
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 ?


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.

Santosh Reddy
26th April 2013, 08:19
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.

rawfool
26th April 2013, 17:01
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


CTableWidget::CTableWidget(QWidget *parent, int nRows, int nCols) :
QTableWidget(parent)
{
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

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 ?

Santosh Reddy
26th April 2013, 23:53
Am I missing something ?
Yes, the question?

rawfool
27th April 2013, 08:22
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.

Santosh Reddy
27th April 2013, 10:23
1. Make sure class definition has Q_OBJECT macro
2. Is there any message in the application output?

rawfool
27th April 2013, 15:08
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.

pradeepreddyg95
28th April 2013, 12:00
once try with cellClicked ( int row, int column ) signal ... and check ...

rawfool
29th April 2013, 06:19
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.