一、目的
通过选择下拉框ComBoBox控件中的不同内容,从而切换StackedWidget控件进行多个页面切换的, 每个页面都可以放自己的控件,然后实现相关的功能。
二、实现
1 . 声明槽函数:
private slots:
void slot_btnOffsetMethod();
2 . 连接下拉框选择信号currentIndexChanged(int)(选择下拉框的不同内容即对应的不同索引值会触发该信号)与槽函数:
connect(ui.cmbOffsetMethod, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_btnOffsetMethod()));
3 . 初始化赋值:
ui.cmbOffsetMethod->setCurrentIndex(); // 0, 1, 2
4 . 实现槽函数:
方式一:void GetMountOffset::slot_btnOffsetMethod()
{
//这里通过下拉框序列选择,也可通过下拉框内容选择
if (ui.cmbOffsetMethod->currentIndex() == 0) {
ui.stackedWidget->setCurrentIndex(0); //控件第一页
}
else if (ui.cmbOffsetMethod->currentIndex() == 1){
ui.stackedWidget->setCurrentIndex(1); //控件第二页
}
}
方式二:
connect(ui.cmbColorChannelInsp, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int channel) {
//这里通过下拉框序列选择,也可通过下拉框内容选择
ui.stackedWidget->setCurrentIndex(channel );
});
三、效果
四、其他
ComboBox
下拉框禁用某一个下拉选项的操作方法:Qt 禁用ComboBox下拉选项。