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

C++ and QML



  • Hi everyone,

    I am having trouble with something that is probably so simple but I cant seem to figure it out...I have a MainWindow that is a QML object which has a child Item as a separate QML file. When I click a button on the main window, this child item becomes visible and some data is dynamically displayed inside a text item within it, which gets updated every second. The data is generated with a c++ class object that I have registered as a QML object and everything work fine...

    The problem is, now I want to have access to the same data that appears in the child window and also display it in a text item on the main window....I assumed its simple and all i need to do is exactly the same i did for the child window, and that is to include the C++ registered object as a child item in the main window, but for some reason the data is not updating on the main window... any ideas why this might be happening?

    I am very new to Qt so apologies for my ignorance :D
    I hope what i wrote makes sense


  • Moderators

    @AB_TrP

    very hard to tell without any code 😉 can you show as the "MainWindow" file ?

    an other instance of the cpp class can be a way but it may as well fail, depending on how you registered the class.

    There are QML was to forward a property as well. Many ways to the desired outcome

    code example will help tremendously



  • @J-Hilk
    Yes sure, here is the snippet of the code Im using in the MainWindow:

    @MainWindow.qml

    import IGPSData 1.1
        Text
        {
            id:txtData
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 80
            text: cGPSData.getGPSData()
            visible: true
            font.family:  "Helvetica [Cronyx]"
            font.bold: true
            font.pixelSize: parent.width * 0.08
            color: "#ffffff"
    
    
        }
    // this is the registered C++ class,
        CGPSData{
            id: cGPSData
        }
    
    
        Connections
        {
             target: cGPSData
             onSigGPSDataChanged:
             {
                 txtData.text= cGPSData.getGPSData()
             }
        }
    
    

    I have the exact same code in the child window and that works just fine



  • @J-Hilk

    as for forwarding a property, I came across the alias property but i think this only works when you want to forward a property from the parent to the children and not the other direction as I want...am I correct?



  • @AB_TrP Your QML code seems strange to me, can you show the header of CGPSData?

    For me, it should be something like:

    class CGPSData : public QObject
    {
         Q_OBJECT
    
         Q_PROPERTY(QString gpsData  READ getGPSData NOTIFY gpsDataChanged)
    public
       explicit CGPSData(QObject * parent = 0);
    
       QString getGPSData() const { return m_gpsData; }
       void setGpsData(const QString &gpsData)
       {
            if(gpsData != m_gpsData)
            {
                m_gpsData = gpsData;
                emit gpsDataChanged();
            }
       }
    signals:
        void gpsDataChanged();
    
    private:
        QString m_gpsData;
    };
    

    And then, in your QML

    import IGPSData 1.1
    // this is the registered C++ class,
        CGPSData{
            id: cGPSData
        }
        
        Text
        {
            id:txtData
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 80
            text: cGPSData.gpsData
            visible: true
            font.family:  "Helvetica [Cronyx]"
            font.bold: true
            font.pixelSize: parent.width * 0.08
            color: "#ffffff"
        }
    
    

Log in to reply