我有一个组合框,我调用如下:
QComboBox *comboBox_test ;
comboBox_test = new QComboBox(this);
comboBox_test ->setGeometry(QRect(10, 10, 50, 20));
comboBox_test ->insertItems(0, QStringList() << "A" << "B");
我想做的是将“B”设置为默认值。
我没有找到添加允许我这样做的行代码的方法。
最佳答案
根据您提供的示例,您有两种选择。可以直接用setCurrentIndex()如果您知道索引,或者首先使用 findText 检索索引
因此最初你可以使用
comboBox_test->setCurrentIndex(1);
稍后如果您想在屏幕上重置为“B”
int index = comboBox_test->findText("B"); //use default exact match
if(index >= 0)
comboBox_test->setCurrentIndex(index);
关于c++ - Qt/C++ 中的 ComboBox 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423235/