首页 > 解决方案 > 从 Qt 中的 c++ 文件快速填充组合框项目

问题描述

我是 QT QML 的新手。我在 C++ 文件(audioinput.cpp)中有一个字符串列表,在 QML 文件(voiceCall.qml)中有一个组合框。我需要使用该列表的字符串填充此组合框的项目。

我的列表定义是->

List<Qstring> lst .

我的组合框定义是:

ComboBox {
         id: comboBox
         x: 200
         y: 95
         objectName:  "speakers"
         width: 244
         height: 32
         model: []
}

标签: c++qtcomboboxqml

解决方案


C++:

QQuickView view;
QStringList comboBoxModel = { "one", "two", "three" };
view.rootContext()->setContextProperty("comboBoxModel", &comboBoxModel);
view.setSource("main.qml");
view.show();

QML:

Combobox {
    model: comboBoxModel
}

QStringList 继承自 QList<QString>。


推荐阅读