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.
  • J.HilkJ Offline
    J.HilkJ Offline
    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 Offline
        J.HilkJ Offline
        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 Offline
            J.HilkJ Offline
            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 Offline
                J.HilkJ Offline
                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