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. Calling a variable from C++ in QML
Forum Updated to NodeBB v4.3 + New Features

Calling a variable from C++ in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
16 Posts 4 Posters 1.8k 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.
  • mamoudM mamoud

    I have a header file, and i defined the class, the signals as follows:
    Mysystem.h

    class Mysystem : public SystemCan
    {
    Q_OBJECT
    Q_PROPERTY(QString pNumber MEMBER m_pNumber NOTIFY pNumberChanged)
    
    public:
    explicit Mysystem(quint8 x, quint64 y, QObject *parent = 0);
    
    signals:
        void pNumberChanged();
    
    private:
        QString m_pNumber;
    };
    
    

    and functions are set in .h file and .cpp file as well.

    When I tried to get the value of pNumber I did this also in my main c++ file:

    main.cpp

    Mysystem mysystem;  // but here i got an error because i should give 2 arguments, but i dont know how to do this
    
    
    viewer.rootContext()->setContextProperty("thepnumber", &mysystem);
    
    

    In my QML file i just want to read this variable like this:

    console.log (thepnumber.pNumber)
    

    I wonder how to solve this argument problem, or if there is another way to solve this, i would appreciate that

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

    @mamoud To create the instance:

    Mysystem mysystem(0,1); // x= 0, y =1
    
    viewer.rootContext()->setContextProperty("thepnumber", &mysystem);
    
    

    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
    5
    • mamoudM mamoud

      I have a header file, and i defined the class, the signals as follows:
      Mysystem.h

      class Mysystem : public SystemCan
      {
      Q_OBJECT
      Q_PROPERTY(QString pNumber MEMBER m_pNumber NOTIFY pNumberChanged)
      
      public:
      explicit Mysystem(quint8 x, quint64 y, QObject *parent = 0);
      
      signals:
          void pNumberChanged();
      
      private:
          QString m_pNumber;
      };
      
      

      and functions are set in .h file and .cpp file as well.

      When I tried to get the value of pNumber I did this also in my main c++ file:

      main.cpp

      Mysystem mysystem;  // but here i got an error because i should give 2 arguments, but i dont know how to do this
      
      
      viewer.rootContext()->setContextProperty("thepnumber", &mysystem);
      
      

      In my QML file i just want to read this variable like this:

      console.log (thepnumber.pNumber)
      

      I wonder how to solve this argument problem, or if there is another way to solve this, i would appreciate that

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #4

      @mamoud

      https://www.learncpp.com/cpp-tutorial/85-constructors/


      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
      4
      • VRoninV VRonin

        @mamoud said in Calling a variable from C++ in QML:

        Q_PROPERTY(QString pNumber MEMBER m_pNumber NOTIFY pNumberChanged)

        This will not do what you think it will do. Q_PROPERTY doesn't do any magic on the NOTIFY argument, it will not get automatically triggered if you set the property, you should split it into READ and WRITE and implement emitting the signal in the WRITE method

        @mamoud said in Calling a variable from C++ in QML:

        but here i got an error because i should give 2 arguments, but i dont know how to do this

        Just pass them to the constructor: Mysystem mysystem(0,0);

        mamoudM Offline
        mamoudM Offline
        mamoud
        wrote on last edited by
        #5

        @VRonin

        thanks
        do you mean like this:

        Q_PROPERTY(QString pNumber READ pNumber_read WRITE pNumber_write NOTIFY pNumber_changed)
        

        and adding this as well:

        public: QVariant pNumber_read() const;
        public slots: void pNumber_write(QString value);  // i am not sure about this
        signals: void pNumber_changed();
        private: QString m_pNumber;
        
        VRoninV 1 Reply Last reply
        0
        • mamoudM mamoud

          @VRonin

          thanks
          do you mean like this:

          Q_PROPERTY(QString pNumber READ pNumber_read WRITE pNumber_write NOTIFY pNumber_changed)
          

          and adding this as well:

          public: QVariant pNumber_read() const;
          public slots: void pNumber_write(QString value);  // i am not sure about this
          signals: void pNumber_changed();
          private: QString m_pNumber;
          
          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #6

          @mamoud

          Q_PROPERTY(QString pNumber READ pNumber WRITE setPNumber NOTIFY pNumberChanged)

          public:
          const QString& pNumber() const {return m_pNumber;}
          void setPNumber(const QString& val){
          if(val==m_pNumber) return;
          m_pNumber=val;
          pNumberChanged(m_pNumber);
          }
          signals:
          void pNumberChanged(const QString& pN);
          private:
          QString m_pNumber;

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          mamoudM 1 Reply Last reply
          4
          • J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #7

            Pro (QtCreator) tip:

            write

            Q_PROPERTY(QString pNumber READ pNumber WRITE setPNumber NOTIFY pNumberChanged)

            right click on Q_PROPERTY
            select Refactor
            click on Generate Missing Q_Property Members

            saves a lot of time


            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
            3
            • VRoninV VRonin

              @mamoud

              Q_PROPERTY(QString pNumber READ pNumber WRITE setPNumber NOTIFY pNumberChanged)

              public:
              const QString& pNumber() const {return m_pNumber;}
              void setPNumber(const QString& val){
              if(val==m_pNumber) return;
              m_pNumber=val;
              pNumberChanged(m_pNumber);
              }
              signals:
              void pNumberChanged(const QString& pN);
              private:
              QString m_pNumber;
              mamoudM Offline
              mamoudM Offline
              mamoud
              wrote on last edited by
              #8

              Great thank you,

              but i still have this problem, that i cannot read the value in QML when i write this:

              console.log (thepnumber.pNumber)
              

              it shows nothing

              J.HilkJ 1 Reply Last reply
              0
              • mamoudM mamoud

                Great thank you,

                but i still have this problem, that i cannot read the value in QML when i write this:

                console.log (thepnumber.pNumber)
                

                it shows nothing

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #9

                @mamoud said in Calling a variable from C++ in QML:

                Great thank you,

                but i still have this problem, that i cannot read the value in QML when i write this:

                console.log (thepnumber.pNumber)
                

                it shows nothing

                more context please, where exactly is this called?


                //General solution
                Connections{
                target: thepnumber

                onpNumberChanged: console.log(thepnumber.pNumber)
                }


                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
                • mamoudM Offline
                  mamoudM Offline
                  mamoud
                  wrote on last edited by mamoud
                  #10

                  I have a QML called PageInfo.qml

                  Rectangle {
                      id: infopage
                  
                      function pageEnter() {
                          console.log("The part is: " + thepnumber.pNumber)
                      }
                  
                  J.HilkJ 1 Reply Last reply
                  0
                  • mamoudM mamoud

                    I have a QML called PageInfo.qml

                    Rectangle {
                        id: infopage
                    
                        function pageEnter() {
                            console.log("The part is: " + thepnumber.pNumber)
                        }
                    
                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #11

                    @mamoud said in Calling a variable from C++ in QML:

                    pageEnter

                    and when you call pageEnter?


                    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
                    • mamoudM Offline
                      mamoudM Offline
                      mamoud
                      wrote on last edited by
                      #12

                      Once i enter this page it will be called

                      J.HilkJ 1 Reply Last reply
                      0
                      • mamoudM mamoud

                        Once i enter this page it will be called

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #13

                        @mamoud
                        if you write

                        function pageEnter() {
                        console.log("Enter Function to get Number")
                        console.log("The part is: " + thepnumber.pNumber)
                        }

                        do you see any console logs?


                        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
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #14

                          Did you set a value to m_pNumber? if you never assign anything to it than it's totally natural that the log shows an empty string.

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          mamoudM 1 Reply Last reply
                          2
                          • mamoudM Offline
                            mamoudM Offline
                            mamoud
                            wrote on last edited by
                            #15

                            Yeah i see
                            Enter Function to get Number
                            and also i see
                            The part is:
                            but no value is shown for thepnumber.pNumber

                            1 Reply Last reply
                            0
                            • VRoninV VRonin

                              Did you set a value to m_pNumber? if you never assign anything to it than it's totally natural that the log shows an empty string.

                              mamoudM Offline
                              mamoudM Offline
                              mamoud
                              wrote on last edited by
                              #16

                              @VRonin

                              No but this m_pNumber should get values from a CAN signal (canalyzer) which is set in .cpp file

                              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