qt create 使用QComboBox举例
时间: 2023-09-03 08:08:42 浏览: 143
QComboBox是一个常用的Qt控件,它提供了一个下拉框供用户选择。
以下是一个简单的例子,演示如何使用QComboBox:
```cpp
#include <QComboBox>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
QComboBox *comboBox = new QComboBox();
comboBox->addItem("Item 1");
comboBox->addItem("Item 2");
comboBox->addItem("Item 3");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(comboBox);
window->setLayout(layout);
window->show();
return app.exec();
}
```
在这个例子中,我们创建了一个窗口,然后创建了一个QComboBox并添加了三个选项。我们使用QVBoxLayout将QComboBox添加到窗口中,并显示窗口。
当用户选择一个选项时,可以使用当前选项的索引来获取选项的值:
```cpp
int currentIndex = comboBox->currentIndex();
QString currentItemText = comboBox->currentText();
```
currentIndex返回当前选中项的索引,currentText返回当前选中项的文本。
阅读全文
相关推荐
















