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. Working wit C++ Backends, how acces Qml Object
QtWS25 Last Chance

Working wit C++ Backends, how acces Qml Object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 695 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.
  • P Offline
    P Offline
    Peppi9300
    wrote on last edited by
    #1

    Hello I´m new with QML and develop a embedded application.
    To access the QML Properties I wrote backends in C++.

    //Qml file
    ```import einrichtbackend 1.0
            SpinBox {
                id: spinBox_proben_nr
                x: 350
                y: 30
                width: 180
                height: 40
                wheelEnabled: false
                editable: true
                to: 9999
                value: einrichtbackend.nummer
                onValueChanged: einrichtbackend.setNummer(value)}```
    
    
    ``` // in my main:
     qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd");
    
    
    //C++ Backend Header 
    #include <QObject>
    class EinrichtBackEnd : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int nummer READ nummer WRITE setNummer NOTIFY NummerChanged)
    private:
    static int m_Nummer; //Static so every Object has the same Value
    public:
    int nummer() const;
    signals:
    void NummerChanged(int nummer);
    public slots:
    void setNummer(int Nummer);
    } ;
    
    //C++ Backend cpp
    //global Object
    EinrichtBackEnd o_einrichtbackend;
    // intialize Variable
    int EinrichtBackEnd::m_Nummer = 1;
    EinrichtBackEnd::EinrichtBackEnd(QObject *parent) : QObject(parent)
    { 
    }
    int EinrichtBackEnd::nummer() const
    {
        return m_Nummer;
    }
    void EinrichtBackEnd::setNummer(int Nummer)
    {
        if (m_Nummer == Nummer)
            return;
        m_Nummer = Nummer;
        emit NummerChanged(m_Nummer);
    }
    

    With the global Object I can acces from any File to the Qml Interface.
    My Problem is that I have two Objects one from C++ and one from qml.
    With the c++ object I can change the members but the signals to qml doesn´t work.

    My Question:
    Can I acces in C++to the qml Object or how can I say with my c++ object to the qml interface that the values have changed?

    Sorry for my bad english
    Thanks for your help.

    J.HilkJ 1 Reply Last reply
    0
    • P Peppi9300

      Hello I´m new with QML and develop a embedded application.
      To access the QML Properties I wrote backends in C++.

      //Qml file
      ```import einrichtbackend 1.0
              SpinBox {
                  id: spinBox_proben_nr
                  x: 350
                  y: 30
                  width: 180
                  height: 40
                  wheelEnabled: false
                  editable: true
                  to: 9999
                  value: einrichtbackend.nummer
                  onValueChanged: einrichtbackend.setNummer(value)}```
      
      
      ``` // in my main:
       qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd");
      
      
      //C++ Backend Header 
      #include <QObject>
      class EinrichtBackEnd : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(int nummer READ nummer WRITE setNummer NOTIFY NummerChanged)
      private:
      static int m_Nummer; //Static so every Object has the same Value
      public:
      int nummer() const;
      signals:
      void NummerChanged(int nummer);
      public slots:
      void setNummer(int Nummer);
      } ;
      
      //C++ Backend cpp
      //global Object
      EinrichtBackEnd o_einrichtbackend;
      // intialize Variable
      int EinrichtBackEnd::m_Nummer = 1;
      EinrichtBackEnd::EinrichtBackEnd(QObject *parent) : QObject(parent)
      { 
      }
      int EinrichtBackEnd::nummer() const
      {
          return m_Nummer;
      }
      void EinrichtBackEnd::setNummer(int Nummer)
      {
          if (m_Nummer == Nummer)
              return;
          m_Nummer = Nummer;
          emit NummerChanged(m_Nummer);
      }
      

      With the global Object I can acces from any File to the Qml Interface.
      My Problem is that I have two Objects one from C++ and one from qml.
      With the c++ object I can change the members but the signals to qml doesn´t work.

      My Question:
      Can I acces in C++to the qml Object or how can I say with my c++ object to the qml interface that the values have changed?

      Sorry for my bad english
      Thanks for your help.

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

      hi @Peppi9300 said in Working wit C++ Backends, how acces Qml Object:

      //Qml file

      import einrichtbackend 1.0
              SpinBox {
                  id: spinBox_proben_nr
                  x: 350
                  y: 30
                  width: 180
                  height: 40
                  wheelEnabled: false
                  editable: true
                  to: 9999
                  value: einrichtbackend.nummer
                  onValueChanged: einrichtbackend.setNummer(value)}
      

      The way it is currently, it shouldn't work, you have the c++ class as a RegisteredType -> you need an actual object of it in your QML file

      import einrichtbackend 1.0
               SpinBox {
                   id: spinBox_proben_nr
                   x: 350
                   y: 30
                   width: 180
                   height: 40
                   wheelEnabled: false
                   editable: true
                   to: 9999
       
                   EinrichtBackEnd{
                   id: backEnd
                   }
                   value: backEnd.nummer
                   onValueChanged: einrichtbackend.setNummer(value)}
      

      now, the qml side is updated when ever nummerChanged() is emited from your c++ file and inside the cpp class nummerChanged() is also emitted, when the QML file changes the nummer property (backEnd.nummer = 1112324)

      PS:
      Nice Denglisch nomenclature you have there x)


      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Peppi9300
        wrote on last edited by
        #3

        @J-Hilk thanks for your reply.
        The Object in the qml I only forget to post in my question.

        My Problem is that I want to use the o_einrichtbackend which is a global Object in my Backend to make changes in my GUI.
        But the NummerChanged Signal does not work.
        Can I acces the Object from QML in C++?

        1 Reply Last reply
        0
        • DuBuD Offline
          DuBuD Offline
          DuBu
          wrote on last edited by
          #4

          "My Problem is that I have two Objects one from C++ and one from qml."

          No, you don't. At least I can't see where the object is instantiated in QML!
          Before doing something like that:

          value: einrichtbackend.nummer
          

          you've got to instantiate the object like so:

          EinrichtBackEnd {
            id: einrichtbackend
          }
          

          The line:

          qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd");
          

          in your main.cpp file only registers the type in QML. Now QML knows only the type, it doesn't instantiate the object for you!

          Anyway, if you already instantiated the global object "o_einrichtbackend" in C++, you can inject it into the root context of QML:

          // qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd");
          engine.rootContext()->setContextProperty(QStringLiteral("einrichtbackend"), o_einrichtbackend);
          

          Now the object "einrichtbackend" is known in the QML context and you can use it the way you already did.

          P.S.: Englisch und Deutsch gemischt in Klassen-/Variablennamen ist bäh! ;-)

          1 Reply Last reply
          4
          • P Offline
            P Offline
            Peppi9300
            wrote on last edited by
            #5

            @DuBu thanks for your reply
            I had instantiate a object in qml...
            This was the answer for my Problem:
            engine.rootContext()->setContextProperty(QStringLiteral("einrichtbackend"), o_einrichtbackend);
            Thanks

            DuBuD 1 Reply Last reply
            0
            • P Peppi9300

              @DuBu thanks for your reply
              I had instantiate a object in qml...
              This was the answer for my Problem:
              engine.rootContext()->setContextProperty(QStringLiteral("einrichtbackend"), o_einrichtbackend);
              Thanks

              DuBuD Offline
              DuBuD Offline
              DuBu
              wrote on last edited by
              #6

              @Peppi9300 Don't forget to remove the instantiation in QML, you already did it in C++!

              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