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++ data didn't update in Qml
Forum Updated to NodeBB v4.3 + New Features

C++ data didn't update in Qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
15 Posts 2 Posters 1.1k 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 Offline
    D Offline
    dziko147
    wrote on last edited by
    #1

    Hello guys ,
    I have a frontend Qml , i want to display some data .
    What i do is i create a backend.cpp class

    #ifndef BACKEND_H
    #define BACKEND_H
    
    #include <QObject>
    #include <QQmlEngine>
    
    class Backend : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString directiontext READ getdirectiontext WRITE setdirectiontext NOTIFY directiontextChanged)
    QML_ELEMENT
    
    public:
    explicit Backend(QObject *parent = nullptr);
    QString getdirectiontext() {return m_directiontext;}
    ;}
    
    }
    //Setter methode //
    
    void setdirectiontext(QString text);
    signals:
    void directiontextChanged();
    
    private :
    QString m_directiontext;
    
    };
    
    #endif // BACKEND_H```
    
    

    //Backend.cpp
    #include"backend.h"
    #include<QDebug>
    Backend::Backend(QObject *parent ) :QObject(parent)
    {
    m_directiontext="test direct";

    void Backend::setdirectiontext(QString text)
    {
    qDebug("signal direction called");
    if(m_directiontext == text)
    return;
    m_directiontext = text;
    emit directiontextChanged();
    }

     
    I used a quickwidget to display Qml file 
    here is my Mainwindow 
    

    qmlRegisterType<Backend>("com.beckend", 1, 0, "Myback");

    m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));```

    and my qmlfile

    import com.beckend 1.0
    Myback{
        id:back
    }
    Label {
        id: label5
        x: 879
        y: 192
        text: back.directiontext
    }
    

    I have another class for change , so i create a command.cpp class and this is my code .

    Backend back;
    QString Mode= ui->modecommande->currentText(); // this is a combobox
    qDebug() << Mode << "this is Mode select commande" ;
    back.setdirectiontext(Mode);
    

    when the combobox value change the data should change in my qmlfile .

    Currently , i couldn't do this .

    Any help please
    thank you

    KroMignonK 1 Reply Last reply
    0
    • D dziko147

      Hello guys ,
      I have a frontend Qml , i want to display some data .
      What i do is i create a backend.cpp class

      #ifndef BACKEND_H
      #define BACKEND_H
      
      #include <QObject>
      #include <QQmlEngine>
      
      class Backend : public QObject
      {
      Q_OBJECT
      Q_PROPERTY(QString directiontext READ getdirectiontext WRITE setdirectiontext NOTIFY directiontextChanged)
      QML_ELEMENT
      
      public:
      explicit Backend(QObject *parent = nullptr);
      QString getdirectiontext() {return m_directiontext;}
      ;}
      
      }
      //Setter methode //
      
      void setdirectiontext(QString text);
      signals:
      void directiontextChanged();
      
      private :
      QString m_directiontext;
      
      };
      
      #endif // BACKEND_H```
      
      

      //Backend.cpp
      #include"backend.h"
      #include<QDebug>
      Backend::Backend(QObject *parent ) :QObject(parent)
      {
      m_directiontext="test direct";

      void Backend::setdirectiontext(QString text)
      {
      qDebug("signal direction called");
      if(m_directiontext == text)
      return;
      m_directiontext = text;
      emit directiontextChanged();
      }

       
      I used a quickwidget to display Qml file 
      here is my Mainwindow 
      

      qmlRegisterType<Backend>("com.beckend", 1, 0, "Myback");

      m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
      m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));```

      and my qmlfile

      import com.beckend 1.0
      Myback{
          id:back
      }
      Label {
          id: label5
          x: 879
          y: 192
          text: back.directiontext
      }
      

      I have another class for change , so i create a command.cpp class and this is my code .

      Backend back;
      QString Mode= ui->modecommande->currentText(); // this is a combobox
      qDebug() << Mode << "this is Mode select commande" ;
      back.setdirectiontext(Mode);
      

      when the combobox value change the data should change in my qmlfile .

      Currently , i couldn't do this .

      Any help please
      thank you

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

      @dziko147 said in C++ data didn't update in Qml:

      when the combobox value change the data should change in my qmlfile .
      Currently , i couldn't do this .

      This cannot work as it is.

      Backend back;
      QString Mode= ui->modecommande->currentText(); // this is a combobox
      qDebug() << Mode << "this is Mode select commande" ;
      back.setdirectiontext(Mode);
      

      This will create a local variable back updates m_directiontext and destroy the variable on function end.
      Why should QML be aware about this?


      import com.beckend 1.0
      Myback{
          id:back
      }
      

      Will create a QML component back, there is no link with C++ side.


      The right way is to create a global instance of Backend, and register it into QML context.

      qmlRegisterType<Backend>("com.beckend", 1, 0, "Myback");
      
      m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
      // supposing m_ui has a member `m_back`
      m_ui->quickWidget->rootContext()->setContextProperty("backend", &m_back);
      m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));```
      

      Use this reference in QML and then all changes on C++ will be available on QML side.

      Label {
          id: label5
          x: 879
          y: 192
          text: backend.directiontext
      }
      

      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
        #3

        @KroMignon thanks for your reply .
        I tried this solution and it doesn't work . :/

        KroMignonK 1 Reply Last reply
        0
        • D dziko147

          @KroMignon thanks for your reply .
          I tried this solution and it doesn't work . :/

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

          @dziko147 said in C++ data didn't update in Qml:

          I tried this solution and it doesn't work . :/

          Did you change also change the update method?

          QString Mode= ui->modecommande->currentText(); // this is a combobox
          qDebug() << Mode << "this is Mode select commande" ;
          ui->m_back.setdirectiontext(Mode);
          

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

          D 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @dziko147 said in C++ data didn't update in Qml:

            I tried this solution and it doesn't work . :/

            Did you change also change the update method?

            QString Mode= ui->modecommande->currentText(); // this is a combobox
            qDebug() << Mode << "this is Mode select commande" ;
            ui->m_back.setdirectiontext(Mode);
            
            D Offline
            D Offline
            dziko147
            wrote on last edited by
            #5
            This post is deleted!
            KroMignonK 1 Reply Last reply
            0
            • D dziko147

              This post is deleted!

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

              @dziko147 said in C++ data didn't update in Qml:

              @KroMignon No ,
              Can you suggest a method ?

              I don't understand what you mean, please be more clear.
              No what?

              • No you don't have change the update method?
              • After changing the update method, the QML didn't be update?

              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
                #7

                @KroMignon the update method is in another class .
                So how can get this m_back :)
                i am really new in Qt developpment .
                So excuse meif my question is stupid :)

                KroMignonK 1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dziko147
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • D dziko147

                    @KroMignon the update method is in another class .
                    So how can get this m_back :)
                    i am really new in Qt developpment .
                    So excuse meif my question is stupid :)

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

                    @dziko147 said in C++ data didn't update in Qml:

                    the update method is in another class .
                    So how can get this m_back :)
                    i am really new in Qt developpment .
                    So excuse meif my question is stupid :)

                    I don't want to hurt you, but this has nothing to do with Qt, this is C++ basic.

                    Do you have any knowledge about C++ programming?
                    Do you know what is a class?
                    Do you know what is a class method or member?

                    Please take time to got basic knowledge about the programming language you want to use.
                    There are many C++ tutorials available, take one and do at least the first step to understand what you are doing.

                    Basic C++ knowledge is mandatory if you want to program with Qt which is a C++ framework.

                    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 ok i will :)
                      but can you provide a way to create this m_back instance :)

                      KroMignonK 1 Reply Last reply
                      0
                      • D dziko147

                        @KroMignon ok i will :)
                        but can you provide a way to create this m_back instance :)

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

                        @dziko147 said in C++ data didn't update in Qml:

                        but can you provide a way to create this m_back instance :)

                        https://www.w3schools.com/cpp/cpp_classes.asp

                        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
                          #12

                          @KroMignon I know how to use class/object .
                          I know how to manipulate c++ .
                          But my problem is how can get this in my second class

                          m_ui->quickWidget->rootContext()->setContextProperty("backend", &m_back);
                          
                          KroMignonK 1 Reply Last reply
                          0
                          • D dziko147

                            @KroMignon I know how to use class/object .
                            I know how to manipulate c++ .
                            But my problem is how can get this in my second class

                            m_ui->quickWidget->rootContext()->setContextProperty("backend", &m_back);
                            
                            KroMignonK Offline
                            KroMignonK Offline
                            KroMignon
                            wrote on last edited by
                            #13

                            @dziko147 said in C++ data didn't update in Qml:

                            But my problem is how can get this in my second class

                            I don't understand which second class you are talking about?

                            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
                              #14
                              This post is deleted!
                              KroMignonK 1 Reply Last reply
                              0
                              • D dziko147

                                This post is deleted!

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

                                @dziko147 said in C++ data didn't update in Qml:

                                So my problem is how can i get the m_back in the commande.cpp

                                Again, this is C++ basic => variable scope and life-cycle:

                                • https://en.cppreference.com/w/cpp/language/lifetime
                                • https://www.geeksforgeeks.org/scope-of-variables-in-c/
                                • http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/3-C/scoping.html

                                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

                                • Login

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