Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

read value from combo box to c++ function



  • 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!


  • Lifetime Qt Champion

    Hi,

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



  • @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;
        }
    
    


  • @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();
    }
    
    

  • Qt Champions 2018

    @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