组合框默认值/ C++

问题描述:

我有一个,我已经叫如下组合框:组合框默认值/ C++

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); 

这里有一个简单的方法调用setCurrentText()而不是setCurrentIndex()

comboBox_test->findText("B"); 

你只需一行即可!并且它是安全的,如果列表中不存在“B”则什么都不会发生。