当currentIndex+1的currentIndex发生变化时,我需要用QComboBox调用一个函数。今天早上我在语法上苦苦挣扎:
// call function readTables(int) when currentIndex changes.
connect(ui->deviceBox, SIGNAL(currentIndexChanged()),
SLOT( readTables( ui->deviceBox->currentIndex()+1) );错误:应为')‘插槽( readTables(ui->deviceBox->currentIndex()+1) );
添加结束语)将不起作用...!
发布于 2015-01-22 00:10:45
First。如果你可以修改函数readTables,那么你可以这样写:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)), SLOT(readTables(int));在readTables中
void MyClass::readTables( int idx ) {
idx++;
// do another stuff
}Second:如果您可以使用Qt 5+和c++11,只需编写:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
[this]( int idx ) { readTables( idx + 1 ); }
);Third:如果您不能修改readTables,也不能使用c++11,那么可以像这样编写您自己的插槽(比如readTables_increment):
void MyClass::readTables_increment( idx ) {
readTables( idx + 1 );
}并将信号连接到它:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
SLOT(readTables_increment(int))
);发布于 2015-01-22 00:05:59
QComboBox::currentIndexChanged要求将QString或int作为单个参数。这里有两个错误:
currentIndexChanged() SLOT作为需要插槽签名的插槽参数进行传递;相反,您正在尝试传递一个不允许的“动态”参数。如果你对使用C++ lambdas没意见的话,@borisbn的建议是非常好的。否则,您将不得不使用int参数声明一个新的插槽:
void ThisClass::slotCurrentIndexChanged(int currentIndex) {
ui->deviceBox->readTables(ui->deviceBox->currentIndex() + 1);
}https://stackoverflow.com/questions/28071461
复制相似问题