I need to call a function with currentIndex+1 when the currentIndex of a QComboBox changes. I'm struggling with the syntax this morning:
// call function readTables(int) when currentIndex changes.
connect(ui->deviceBox, SIGNAL(currentIndexChanged()),
SLOT( readTables( ui->deviceBox->currentIndex()+1) );
error: expected ')' SLOT( readTables(ui->deviceBox->currentIndex()+1) );
Adding a closing ) won't work...!
First. If you can modify function readTables
then you can just write:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)), SLOT(readTables(int));
and in readTables
void MyClass::readTables( int idx ) {
idx++;
// do another stuff
}
Second: If you can use Qt 5+ and c++11, just write:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
[this]( int idx ) { readTables( idx + 1 ); }
);
Third: If you can't modify readTables
and can't use c++11, write your own slot (say readTables_increment
) like this:
void MyClass::readTables_increment( idx ) {
readTables( idx + 1 );
}
and connect signal to it:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
SLOT(readTables_increment(int))
);
QComboBox::currentIndexChanged
expects either a QString or a int as the single argument. You have 2 errors here:
currentIndexChanged()
which does not existSLOT
as the slot argument which requires the slot signature; rather, you are trying to pass an argument "on-the-fly" which is not something allowed.@borisbn suggestion is pretty good if you are ok with using C++ lambdas.
Otherwise you'll have to declare a new slot with an int
argument:
void ThisClass::slotCurrentIndexChanged(int currentIndex) {
ui->deviceBox->readTables(ui->deviceBox->currentIndex() + 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With