Qt如何获取下拉框对象里面的值
时间: 2023-08-06 09:06:57 浏览: 451
获取Qt中QComboBox下拉框对象里面的值,可以使用QComboBox的currentText()方法来获取当前选中的文本。该方法返回当前下拉框中显示的文本。
下面是一个简单的示例代码:
```cpp
QComboBox *comboBox = new QComboBox(this);
comboBox->addItem("Item 1");
comboBox->addItem("Item 2");
comboBox->addItem("Item 3");
QString currentText = comboBox->currentText();
qDebug() << "Current selected item: " << currentText;
```
输出结果应该为:
```
Current selected item: Item 1
```
如果你想获取当前选中的条目的索引,可以使用QComboBox的currentIndex()方法来获取。该方法返回当前选中条目的索引,如果没有选中条目,则返回-1。
下面是一个简单的示例代码:
```cpp
QComboBox *comboBox = new QComboBox(this);
comboBox->addItem("Item 1");
comboBox->addItem("Item 2");
comboBox->addItem("Item 3");
int currentIndex = comboBox->currentIndex();
qDebug() << "Current selected item index: " << currentIndex;
```
输出结果应该为:
```
Current selected item index: 0
```
希望这个回答能够帮助到你。如果你有任何其他问题,都可以继续问我。
阅读全文
相关推荐

















