Important: Please read the Qt Code of Conduct -

QML ComboBox access in C++ with QComboBox

  • Hello,

    I've created a ComboBox inside of QML that I'm wanting to manipulate in my main.cpp using a QComboBox object. Is this possible using the following?

    I need to get the current selected text in C++ to perform some logic. The ComboBox is populated with a JSON file.


    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        // Hide mouse curser.
        // Load gauge UI.
        QQmlEngine engine;
        QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
        object = component.create();
        QObject *comboBoxTracks = object->findChild<QObject*>("cboTracks");
        if (comboBoxTracks)
            QStringList list = jsonDoc.object().keys();
            comboBoxTracks->setProperty("model", list);


    ComboBox {
                id: cboTracks
                objectName: "cboTracks"
                currentIndex: -2
                x: 8
                y: 365
                width: 200
                height: 30
                style: ComboBoxStyle {
                    textColor: "#000000"
                    background: Rectangle {
                        color: "#000000"
                        border.width: 1
                        border.color: "#ffffff"
                        antialiasing: true
                    label: Label {
                        text: "Select Track..."
                        verticalAlignment: Text.AlignVCenter
                        font.pixelSize: 14
                        color: "#ffffff"

  • @rmfabac Have a look into these documentation. Talks about so many way for QML and C++ interaction.


Log in to reply