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. C++ and QML
Forum Updated to NodeBB v4.3 + New Features

C++ and QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 442 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    AB_TrP
    wrote on last edited by
    #1

    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

    J.HilkJ 1 Reply Last reply
    0
    • A AB_TrP

      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

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      A 1 Reply Last reply
      1
      • A Offline
        A Offline
        AB_TrP
        wrote on last edited by AB_TrP
        #3

        @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

        KroMignonK 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @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

          A Offline
          A Offline
          AB_TrP
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A AB_TrP

            @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

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @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"
                }
            
            

            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

            • Login

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