PDA

View Full Version : Creating Slot for Setting Number of Rows in QTableWidget



PeterMW88
15th April 2014, 00:31
I'm using a spinbox to set the number of rows and columns in some QTableWidgets. I did what is described in this thread: http://www.qtcentre.org/threads/25736-Can-I-use-a-spinBox-to-control-the-number-of-rows-in-a-QTableWidget

However, I have multiple tables. Can I make a slot which will work for all the tables? The slot function needs access to the specific table it is changing, so I thought about passing a pointer, but the slot needs to have only one parameter, int, in order to work with the signal from the spinbox. So can I "register" my custom slot with the QTableWidget class somehow, so I have access to the table methods in the slot function?

Thanks in advance.

Edit: Maybe I should create a customized version (subclass) of QTableWidget?

ChrisW67
15th April 2014, 01:22
If I read that correctly, you have only one pair of spin boxes that can be used to set the size of one of several tables.

What event triggers application of the new row/column count?
How do you determine which table the dimensions should apply to?

PeterMW88
15th April 2014, 01:35
Hmm... Sorry, it seems I was unclear. I just got it working using a derived class. I've attached a picture which should answer those questions, and copied in the header file which has solved the problem.

I derived from QTableWidget, hooked up the constructors as required, defined a couple new slots, promoted the tables to my derived version, added the custom slot with the edit button in the designer, and was able to hook up the spin boxes as needed.
10284
Header file:

#ifndef MATRIXWIDGET_H
#define MATRIXWIDGET_H

#include <QTableWidget>

class MatrixWidget : public QTableWidget
{
Q_OBJECT

public:
explicit MatrixWidget(QWidget *parent = 0)
: QTableWidget(parent)
{ ;}
MatrixWidget(int rows, int columns, QWidget *parent = 0)
: QTableWidget(rows, columns, parent)
{ ;}

private slots:
void setNumRows(int n)
{
this->setRowCount(n);
}
void setNumColumns(int n)
{
this->setColumnCount(n);
}
};

#endif // MATRIXWIDGET_H