PDA

View Full Version : Can I use a spinBox to control the number of rows in a QTableWidget?



wconstan
17th November 2009, 09:42
Goal:

I am using Qt Creator 1.2.92 and am trying to use the (changed) value of a spinBox widget in my application to dynamically alter the number of rows displayed in a QTableWidget widget.

What I have done:

I have succeeded in mapping the spinBox valueChanged signal to the QTableWidget insertRow slot using the "Edit signals/slots" mode while editing my file test.ui, which results in the following line in ui_test.h:


QObject::connect(spinBox, SIGNAL(valueChanged(int)), tableWidget, SLOT(insertRow(int)));

What I would like to do:


QObject::connect(spinBox, SIGNAL(valueChanged(int)), tableWidget, SLOT(setRowCount(int)));

However, when I run the associated application I get the following error after changing the spinBox value:


Object::connect: No such slot QTableWidget::setRowCount(int) in ui_test.h:137
Object::connect: (sender name: 'spinBox')
Object::connect: (receiver name: 'tableWidget')

I am completely new to Qt and to the Qt Creator and have had only a little exposure to C++. Is there a way to accomplish this? Any help would be most appreciated.

Thanks!

yogeshgokul
17th November 2009, 10:17
Goal:However, when I run the associated application I get the following error after changing the spinBox value:

Object::connect: No such slot QTableWidget::setRowCount(int) in ui_test.h:137
Object::connect: (sender name: 'spinBox')
Object::connect: (receiver name: 'tableWidget')
This is because setRowCount is a property interface(a normal public member function) not a slot.
Create your own slot, and connect it with signal valueChanged of spin box. Then in your slot you can call the setRowCount().

wconstan
17th November 2009, 17:14
Thanks much for the help.

I resolved the problem by defining the following function in my test.cpp function:


void Test::setTableRows()
{
int rows = ui->spinBox->value();
ui->tableWidget->setRowCount(rows);
}


which is declared as a private slot in test.h:



private slots:
void setTableRows();


To get this to work using Qt Creator, I used the Edit signals/slots mode to wire the spinBox to the centralWidget, which automatically brings up a dialog to form the signal/slot mapping. In that dialog, I selected the Edit... button below the QMainWindow list of slots and added my new setTableRows() slot. I then mapped the valueChanged signal to the (newly added) setTableRows slot.

This worked but I also would like to hear any comments on the technique. In general, is this the best way to go about it?

Thanks!