当 QComboBox 的 currentIndex 更改时,我需要使用 currentIndex+1 调用函数。今天早上我在语法上苦苦挣扎:

// call function readTables(int) when currentIndex changes.

connect(ui->deviceBox, SIGNAL(currentIndexChanged()),
   SLOT( readTables( ui->deviceBox->currentIndex()+1) );

错误:预期')'
SLOT(readTables(ui->deviceBox->currentIndex()+1));

添加结束 ) 将不起作用...!

最佳答案

第一个 。如果你可以修改函数 readTables 那么你可以写:

connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)), SLOT(readTables(int));

readTables
void MyClass::readTables( int idx ) {
    idx++;
    // do another stuff
}

第二个 :如果你可以使用 Qt 5+ 和 c++11,只需写:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
    [this]( int idx ) { readTables( idx + 1 ); }
);

第三个 :如果你不能修改 readTables 并且不能使用 c++11,那么像这样写你自己的槽(比如 readTables_increment ):
void MyClass::readTables_increment( idx ) {
    readTables( idx + 1 );
}

并将信号连接到它:
connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
    SLOT(readTables_increment(int))
);

关于qt - QComboBox 连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28071461/

10-10 09:05