Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QML ComboBox access in C++ with QComboBox

    QML and Qt Quick
    2
    2
    1570
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rmfabac last edited by

      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"
                      }
                  }
              }
      
      
      Yashpal 1 Reply Last reply Reply Quote 0
      • Yashpal
        Yashpal @rmfabac last edited by

        @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
        1 Reply Last reply Reply Quote 1
        • First post
          Last post