QComboBox连接

Joc*_*ala 3 qt qcombobox slot

当QComboBox的currentIndex发生变化时,我需要用currentIndex + 1调用一个函数.今天早上我正在努力学习语法:

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

connect(ui->deviceBox, SIGNAL(currentIndexChanged()),
   SLOT( readTables( ui->deviceBox->currentIndex()+1) );
Run Code Online (Sandbox Code Playgroud)

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

添加关闭)将无法正常工作......!

bor*_*sbn 8

首先.如果你可以修改功能readTables那么你可以写:

connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)), SLOT(readTables(int));
Run Code Online (Sandbox Code Playgroud)

并在 readTables

void MyClass::readTables( int idx ) {
    idx++;
    // do another stuff
}
Run Code Online (Sandbox Code Playgroud)

第二:如果你可以使用Qt 5+和c ++ 11,只需写:

connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
    [this]( int idx ) { readTables( idx + 1 ); }
);
Run Code Online (Sandbox Code Playgroud)

第三:如果你不能修改readTables并且不能使用c ++ 11,readTables_increment那就写下你自己的插槽(比如说):

void MyClass::readTables_increment( idx ) {
    readTables( idx + 1 );
}
Run Code Online (Sandbox Code Playgroud)

并将信号连接到它:

connect(ui->deviceBox, SIGNAL(currentIndexChanged(int)),
    SLOT(readTables_increment(int))
);
Run Code Online (Sandbox Code Playgroud)