QComboBox连接
QComboBox connect
当 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) );
添加结束符 ) 不起作用...!
QComboBox::currentIndexChanged
期望 QString or a int 作为单个参数。这里有 2 个错误:
- 您没有连接到任何现有信号,因为您指定的
currentIndexChanged()
不存在
- 您没有传递
SLOT
作为需要插槽签名的插槽参数;相反,您正试图传递一个参数 "on-the-fly",这是不允许的。
如果您可以使用 C++ lambda,@borisbn 的建议非常好。
否则,您必须使用 int
参数声明一个新插槽:
void ThisClass::slotCurrentIndexChanged(int currentIndex) {
ui->deviceBox->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))
);
当 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) );
添加结束符 ) 不起作用...!
QComboBox::currentIndexChanged
期望 QString or a int 作为单个参数。这里有 2 个错误:
- 您没有连接到任何现有信号,因为您指定的
currentIndexChanged()
不存在 - 您没有传递
SLOT
作为需要插槽签名的插槽参数;相反,您正试图传递一个参数 "on-the-fly",这是不允许的。
如果您可以使用 C++ lambda,@borisbn 的建议非常好。
否则,您必须使用 int
参数声明一个新插槽:
void ThisClass::slotCurrentIndexChanged(int currentIndex) {
ui->deviceBox->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))
);