Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML ComboBox access in C++ with QComboBox

QML ComboBox access in C++ with QComboBox

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.8k Views
  • 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 Offline
    R Offline
    rmfabac
    wrote on last edited by
    #1

    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"
                    }
                }
            }
    
    
    YashpalY 1 Reply Last reply
    0
    • R rmfabac

      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"
                      }
                  }
              }
      
      
      YashpalY Offline
      YashpalY Offline
      Yashpal
      wrote on last edited by
      #2

      @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
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved