c++ - Qt/C++ 中的 ComboBox 默认值

标签 c++ qt

我有一个组合框,我调用如下:

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/

相关文章:

android - 在手机(ios android)跨平台QT上调用sqlite数据库路径

qt - 如何在Qt中绘制一个 9-patch 按钮?

c++ - 我需要重新设计我的应用程序吗?

c++ - QML 中一行的间隔项目?

c++ - qt 4.7 removeChild() 和内存

c++ - 如何在 C++ 中使用 boost 制作正则表达式?

c++ - 微软计算器使用的库

c++ - 为什么类的静态成员函数没有 "const-correctness"的概念?

c++ - winsock:使用 localhost (127.0.0.1) 时连接失败并出现错误 10049

c++ - std::malloc 可以创建一个对象吗?总是需要安置新的吗?