Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating c++ data in Qml

Updating c++ data in Qml

Scheduled Pinned Locked Moved Solved General and Desktop
46 Posts 6 Posters 5.9k 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.
  • D dziko147

    Hello , I diplay a Qml in a Qquickwidget .
    1/I have a class called Backend witch send data to my Qml .
    2/And i have a class called UploadCSV which send signal when button clicked to Slot in backend to start fill data .

    My problem is that I can't get data in QML file.

    what is wrong in my code thank you :)

    So this is my code :
    In backend.h

        Q_PROPERTY(qreal valuespeed READ getValue WRITE setValue NOTIFY valueChanged)
        qreal getValue() {return m_Valuespeed;}
        void setValue(qreal value);
        qreal m_Valuespeed;
    void ValueChanged();
    
    

    in Backend.cpp

    
    void Backend::status()
    {
       m_Valuespeed=Outputs[0].toFloat();
        qDebug() << "data" << m_Valuespeed; // here I get the right data
    
    }
    void Backend::setValue(qreal value)
    {
        qDebug("signal valuespeed called");
        if(m_Valuespeed == value)
            return;
        m_Valuespeed = value;
        emit valueChanged();
    }
    

    In UploadCSV.cpp

    Backend *back = new Backend(); // I think that this line is the problem ?
        connect(ui->parambtn_2, SIGNAL(clicked()),back , SLOT(status()),Qt::QueuedConnection);
    

    In MainWindow.cpp

    back = new Backend();
        m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);
        m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
        m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));
    

    In QML

    value: cppObject.valuespeed
    
    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    @dziko147

    You have to call a handler in qml depending on the signal which is issued by the type you are using
    like here: https://doc.qt.io/qt-5/qtqml-syntax-signals.html

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • D dziko147

      Hello , I diplay a Qml in a Qquickwidget .
      1/I have a class called Backend witch send data to my Qml .
      2/And i have a class called UploadCSV which send signal when button clicked to Slot in backend to start fill data .

      My problem is that I can't get data in QML file.

      what is wrong in my code thank you :)

      So this is my code :
      In backend.h

          Q_PROPERTY(qreal valuespeed READ getValue WRITE setValue NOTIFY valueChanged)
          qreal getValue() {return m_Valuespeed;}
          void setValue(qreal value);
          qreal m_Valuespeed;
      void ValueChanged();
      
      

      in Backend.cpp

      
      void Backend::status()
      {
         m_Valuespeed=Outputs[0].toFloat();
          qDebug() << "data" << m_Valuespeed; // here I get the right data
      
      }
      void Backend::setValue(qreal value)
      {
          qDebug("signal valuespeed called");
          if(m_Valuespeed == value)
              return;
          m_Valuespeed = value;
          emit valueChanged();
      }
      

      In UploadCSV.cpp

      Backend *back = new Backend(); // I think that this line is the problem ?
          connect(ui->parambtn_2, SIGNAL(clicked()),back , SLOT(status()),Qt::QueuedConnection);
      

      In MainWindow.cpp

      back = new Backend();
          m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);
          m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
          m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));
      

      In QML

      value: cppObject.valuespeed
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #3

      @dziko147 I hope you are aware that back in UploadCSV.cpp and MainWindow.cpp are 2 different items!

      Another point: in Backend::setValue() you should use qFuzzyCompare to compare float/double value.

      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
      3
      • D Offline
        D Offline
        dziko147
        wrote on last edited by
        #4

        @KroMignon as per usual :D I'm waiting for your response already :D .

        Yes I understood that back in UploadCSV.cpp and MainWindow are 2 different Item .

        I tried to get back from MainWindow (create instance of Mainwindow in UploadCSV) .
        But didn't work .

        Please Can you suggest a way ?

        KroMignonK 1 Reply Last reply
        0
        • D dziko147

          @KroMignon as per usual :D I'm waiting for your response already :D .

          Yes I understood that back in UploadCSV.cpp and MainWindow are 2 different Item .

          I tried to get back from MainWindow (create instance of Mainwindow in UploadCSV) .
          But didn't work .

          Please Can you suggest a way ?

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

          @dziko147 said in Updating c++ data in Qml:

          Yes I understood that back in UploadCSV.cpp and MainWindow are 2 different Item .
          I tried to get back from MainWindow (create instance of Mainwindow in UploadCSV) .
          But didn't work .
          Please Can you suggest a way ?

          It is very difficult/impossible for me to help you, because I don't understand what you are trying to do.
          Please describe your use case.

          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
          0
          • D Offline
            D Offline
            dziko147
            wrote on last edited by
            #6

            KroMignon i have a button in Uploadcsv ui . When is clicket a signal is emmitted to status slot in backend .
            Value is defined in backend .
            So qml should update the c++ data

            KroMignonK 1 Reply Last reply
            0
            • D dziko147

              KroMignon i have a button in Uploadcsv ui . When is clicket a signal is emmitted to status slot in backend .
              Value is defined in backend .
              So qml should update the c++ data

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

              @dziko147 said in Updating c++ data in Qml:

              i have a button in Uploadcsv ui . When is clicket a signal is emmitted to status slot in backend .
              Value is defined in backend .
              So qml should update the c++ data

              Where do you create UploadCSV instance?

              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
              0
              • D Offline
                D Offline
                dziko147
                wrote on last edited by
                #8

                @KroMignon No i didn't create a Uploadcsv instance ?
                I dont need any uploadcsv instance

                KroMignonK SGaistS 2 Replies Last reply
                0
                • D dziko147

                  @KroMignon No i didn't create a Uploadcsv instance ?
                  I dont need any uploadcsv instance

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

                  @dziko147 said in Updating c++ data in Qml:

                  I dont need any uploadcsv instance

                  And I don't understand what you are doing.
                  Sorry I give up.

                  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
                  0
                  • D Offline
                    D Offline
                    dziko147
                    wrote on last edited by
                    #10

                    @KroMignon when signal emmited from uploadcsv there is a slot in backend witch fill data. Then i called a setprepertycontext in mainwindow which integrate backend in qml file

                    1 Reply Last reply
                    0
                    • D dziko147

                      @KroMignon No i didn't create a Uploadcsv instance ?
                      I dont need any uploadcsv instance

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Hi,

                      @dziko147 said in Updating c++ data in Qml:

                      @KroMignon No i didn't create a Uploadcsv instance ?
                      I dont need any uploadcsv instance

                      If you don't need any instance of that class then why do you have it in your code ?

                      From the code you posted, why do you have two Backend instances ? One in MainWindow and one in UploadCSV ?

                      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
                      0
                      • D Offline
                        D Offline
                        dziko147
                        wrote on last edited by
                        #12

                        @SGaist yes two instance of Backend class.
                        the first in UploadCSV.cpp to emit signal when button clicked

                        Backend *back = new Backend(); // I think that this line is the problem ?
                            connect(ui->parambtn_2, SIGNAL(clicked()),back , SLOT(status()),Qt::QueuedConnection);
                        
                        

                        the second in mainwindow.cpp to create a contextproperty :

                        back = new Backend();
                            m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);```
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          And you expect these two unrelated instances to communicate between different widgets from your UI ?

                          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
                          0
                          • D Offline
                            D Offline
                            dziko147
                            wrote on last edited by
                            #14

                            @SGaist said in Updating c++ data in Qml:

                            expect these two unrelated instances to communicate between different widgets from your UI ?

                            no I mentioned that these two instances cause a problem.

                            So can you suggest a way to do this !

                            BTW UploadCSV is a QDialog , Backend is QObject .

                            when I create instance in UploadCSV.h it cause problem !

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              Review your application architecture. There's an issue with what is created where and how they interact with each other.

                              You seem to have an issue with the proper composition of your various classes and GUI elements.

                              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
                              0
                              • D Offline
                                D Offline
                                dziko147
                                wrote on last edited by
                                #16

                                @SGaist there no way to solve it ?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  Yes there is and was already suggested in your other threads. You need to properly create your objects hierarchy.

                                  You were creating a MainWidget object in your dialog trying to get data from it while it was in fact created by a different instance.

                                  You seem to reproduce a similar scheme here.

                                  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
                                  0
                                  • D Offline
                                    D Offline
                                    dziko147
                                    wrote on last edited by
                                    #18

                                    @SGaist In my situation , when I would like to create an Backend instance in UploadCSV to emit signal .
                                    And create Backend instance in MainWindow to set a contextProperty .
                                    what should I do .
                                    Please :)

                                    KroMignonK 1 Reply Last reply
                                    0
                                    • D dziko147

                                      @SGaist In my situation , when I would like to create an Backend instance in UploadCSV to emit signal .
                                      And create Backend instance in MainWindow to set a contextProperty .
                                      what should I do .
                                      Please :)

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

                                      @dziko147 said in Updating c++ data in Qml:

                                      In my situation , when I would like to create an Backend instance in UploadCSV to emit signal .
                                      And create Backend instance in MainWindow to set a contextProperty .
                                      what should I do .
                                      Please :)

                                      You should first explain why you think you have to create a Backend instance in UploadCSV?
                                      It is impossible to understand (for me) what is the purpose of this.
                                      And emitting a signal which is connected to nothing, results to nothing.

                                      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
                                      0
                                      • D Offline
                                        D Offline
                                        dziko147
                                        wrote on last edited by
                                        #20

                                        @KroMignon in uploadcsv ui , i have a button . I want to emit clicked signal connected to a slot in backend which change data (see my code please) .

                                        KroMignonK 1 Reply Last reply
                                        0
                                        • D dziko147

                                          @KroMignon in uploadcsv ui , i have a button . I want to emit clicked signal connected to a slot in backend which change data (see my code please) .

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

                                          @dziko147 said in Updating c++ data in Qml:

                                          in uploadcsv ui , i have a button . I want to emit clicked signal connected to a slot in backend which change data (see my code please) .

                                          Your code is incomprehensible!
                                          Please learn C++ basics, like what are classes, class members, class methods and class instances.

                                          I don't understand what is your problem, on click event the method status() instance back which is created in UploadCSV.cpp will be called and you should see a debug message on console.

                                          I don't understand why you expect anything on QML side as this instance is not exposed to QML.

                                          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