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. read value from combo box to c++ function

read value from combo box to c++ function

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlc++comboboxsignalsignal & slot
5 Posts 4 Posters 1.4k 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.
  • T Offline
    T Offline
    texasRanger
    wrote on 15 Jun 2021, 19:57 last edited by
    #1

    Hello,
    I have a simple combo box with three string values. When the combo box is changed to one of these values then i need to call a function in c++ that will update a member variable that holds the value of the value in the combo box. I'm still learning signals and what not and would love some direction of how to signal that the combo box has changed and then update the member variable in c++. Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Jun 2021, 20:00 last edited by
      #2

      Hi,

      There's a whole chapter in Qt's documentation about integrating QML and C++.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T Offline
        T Offline
        texasRanger
        wrote on 15 Jun 2021, 21:59 last edited by texasRanger
        #3

        @SGaist
        I have been trying to connect the qml signal of the combo box being changed to the itemChanged slot in c++. but I have had no luck.

        //main.cpp
        
        using namespace Esri::ArcGISRuntime;
        
        int main(int argc, char *argv[])
        {
            QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            QGuiApplication app(argc, argv);
        
            qmlRegisterType<MapQuickView>("Esri.Display_a_map", 1, 0, "MapView");
        
            qmlRegisterType<Display_a_map>("Esri.Display_a_map", 1, 0, "Display_a_map");
        
            QQmlApplicationEngine engine;
        
            QQuickView view;
            Display_a_map disp;
        
            QQuickItem *item = view.rootObject()->findChild<QQuickItem*>("comboBox");
            if(item) {
                QObject::connect(item, SIGNAL(activated(int)), &disp, SLOT(itemChanged(int)));
            }
        
            // Add the import Path
            engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
        
            // Set the source
            engine.load(QUrl("qrc:/qml/main.qml"));
        
            return app.exec();
        }
        
        //Display_a_map.h
        
        public slots:
            void itemChanged(const int &val)
            {
                qDebug() << "Called the c++ slot with value" << val;
            }
        
        
        K G 2 Replies Last reply 16 Jun 2021, 05:36
        0
        • T texasRanger
          15 Jun 2021, 21:59

          @SGaist
          I have been trying to connect the qml signal of the combo box being changed to the itemChanged slot in c++. but I have had no luck.

          //main.cpp
          
          using namespace Esri::ArcGISRuntime;
          
          int main(int argc, char *argv[])
          {
              QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              QGuiApplication app(argc, argv);
          
              qmlRegisterType<MapQuickView>("Esri.Display_a_map", 1, 0, "MapView");
          
              qmlRegisterType<Display_a_map>("Esri.Display_a_map", 1, 0, "Display_a_map");
          
              QQmlApplicationEngine engine;
          
              QQuickView view;
              Display_a_map disp;
          
              QQuickItem *item = view.rootObject()->findChild<QQuickItem*>("comboBox");
              if(item) {
                  QObject::connect(item, SIGNAL(activated(int)), &disp, SLOT(itemChanged(int)));
              }
          
              // Add the import Path
              engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
          
              // Set the source
              engine.load(QUrl("qrc:/qml/main.qml"));
          
              return app.exec();
          }
          
          //Display_a_map.h
          
          public slots:
              void itemChanged(const int &val)
              {
                  qDebug() << "Called the c++ slot with value" << val;
              }
          
          
          K Offline
          K Offline
          KroMignon
          wrote on 16 Jun 2021, 05:36 last edited by
          #4

          @texasRanger said in read value from combo box to c++ function:

          I think you should load the QML before trying to find the children ;)

          int main(int argc, char *argv[])
          {
              QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              QGuiApplication app(argc, argv);
          
              qmlRegisterType<MapQuickView>("Esri.Display_a_map", 1, 0, "MapView");
          
              qmlRegisterType<Display_a_map>("Esri.Display_a_map", 1, 0, "Display_a_map");
          
              QQmlApplicationEngine engine;
          
              // Add the import Path
              engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
          
              // Set the source
              engine.load(QUrl("qrc:/qml/main.qml"));
          
              Display_a_map disp;
              QQuickItem *item = engine.rootObject()->findChild<QQuickItem*>("comboBox");
              if(item) {
                  QObject::connect(item, SIGNAL(activated(int)), &disp, SLOT(itemChanged(int)));
              }
          
              return app.exec();
          }
          
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1
          • T texasRanger
            15 Jun 2021, 21:59

            @SGaist
            I have been trying to connect the qml signal of the combo box being changed to the itemChanged slot in c++. but I have had no luck.

            //main.cpp
            
            using namespace Esri::ArcGISRuntime;
            
            int main(int argc, char *argv[])
            {
                QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                QGuiApplication app(argc, argv);
            
                qmlRegisterType<MapQuickView>("Esri.Display_a_map", 1, 0, "MapView");
            
                qmlRegisterType<Display_a_map>("Esri.Display_a_map", 1, 0, "Display_a_map");
            
                QQmlApplicationEngine engine;
            
                QQuickView view;
                Display_a_map disp;
            
                QQuickItem *item = view.rootObject()->findChild<QQuickItem*>("comboBox");
                if(item) {
                    QObject::connect(item, SIGNAL(activated(int)), &disp, SLOT(itemChanged(int)));
                }
            
                // Add the import Path
                engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
            
                // Set the source
                engine.load(QUrl("qrc:/qml/main.qml"));
            
                return app.exec();
            }
            
            //Display_a_map.h
            
            public slots:
                void itemChanged(const int &val)
                {
                    qDebug() << "Called the c++ slot with value" << val;
                }
            
            
            G Offline
            G Offline
            GrecKo
            Qt Champions 2018
            wrote on 16 Jun 2021, 08:16 last edited by
            #5

            @texasRanger said in read value from combo box to c++ function:

            I have been trying to connect the qml signal of the combo box being changed to the itemChanged slot in c++. but I have had no luck.

            Don't do that.
            Do like in the link posted by SGaist.

            Some links about the reason why you shouldn't reach into QML from C++:

            https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
            http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
            https://youtu.be/vzs5VPTf4QQ?t=23m20s

            1 Reply Last reply
            1

            1/5

            15 Jun 2021, 19:57

            • Login

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