Important: Please read the Qt Code of Conduct -

Populating qml combobox

  • How do I populate combobox? I have QStringList model in c++.
    my main.cpp
    @QGuiApplication app(argc, argv);

    QQmlEngine engine;
    MainWindowMC mainMC;
    engine.rootContext()->setContextProperty("mainMC", &mainMC);
    QQmlComponent component(&engine, QUrl::fromLocalFile("qml/convQML/main.qml"));
    qDebug() << component.errors();
    QQuickWindow* window =  qobject_cast<QQuickWindow*> (component.create());
    return app.exec();


    @ComboBox {
    id: typebox

            anchors.left: text1.right
            anchors.leftMargin: 5
            width: 70
            height: 23
            anchors.topMargin: 37
            model: mainMC.typemodel
            Component.onCompleted: {

    MainWindowMC :
    @class MainWindowMC : public MainM

    // Q_OBJECT

    Q_PROPERTY(QStringList typemodel READ typemodel)


    QStringList typemodel();
    void typeGenerator();

    QStringList m_typemodel;


    typemodel() is getter function for m_typemodel.

    How do I port QStringList to qml?
    And overall, what is the best way to populate qml typebox (or any other model-based component) from c++ model generator?

  • Looking at the ComboBox behaviour, it gets filled through its "model" property by using a "ListModel".

    Now when you define the "ListModel" in QML let's say something like:
    ListModel {
    id: cbItems
    ListElement { text: "Banana"; color: "Yellow" }
    ListElement { text: "Apple"; color: "Green" }
    ListElement { text: "Coconut"; color: "Brown" }

    The ComboBox control will use the contents of the tag "text" to fill it's elements to be displayed.

    So I would suppose that you will need to provide such a model through your C++ class, I don't know maybe a Qt stringList or AbstractModel in C++ from where you could provide your data in the format required by the QML ComboBox control.

Log in to reply