gpt4 book ai didi

qt - QComboBox 连接

转载 作者:行者123 更新时间:2023-12-04 13:03:21 28 4
gpt4 key购买 nike

当 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com