QT开发中文网

c++ qt combobox

A combobox in Qt is a drop-down menu that allows the user to select one option from a list of options.

To create a combobox in Qt, you can use the QComboBox class. Here’s an example of how to create a simple combobox with three options:

#include <QtWidgets/QApplication>
#include <QtWidgets/QComboBox>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QComboBox comboBox;
    comboBox.addItem("Option 1");
    comboBox.addItem("Option 2");
    comboBox.addItem("Option 3");

    comboBox.show();
    return app.exec();
}

In this example, we first include the necessary headers and create a QApplication object. We then create a QComboBox object and add three items to it using the addItem() method. Finally, we show the combobox and start the event loop by calling app.exec().

You can also set the current selection of the combobox programmatically using the setCurrentIndex() method:

comboBox.setCurrentIndex(1); // sets the second option as selected

And you can retrieve the index or text of the currently selected item using currentIndex() or currentText():

int selectedIndex = comboBox.currentIndex();
QString selectedText = comboBox.currentText();

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=914

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?