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.

    main.cpp

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        // Hide mouse curser.
        QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
    
        // 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);
        }
    

    main.qml

    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.

    1. http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html
    2. http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.