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. Q_PROPERTY
Qt 6.11 is out! See what's new in the release blog

Q_PROPERTY

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k 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.
  • G Offline
    G Offline
    Gunkut
    wrote on last edited by
    #1

    Hello I want to use a variable from .qml file in a .cpp file. I want to do something according to value of (which is in .qml file) isCamRezistans in C++. So I want to put it's value into m_isCamRezistans. For that I use Q_PROPERTY as follows:

    class CAN : public QObject
    {
       Q_OBJECT
    
    public:
       Q_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged)
       Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans)
    
    private:
       quint32 bitrate;
       bool m_isCamRezistans
    

    Q_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged) does not give any errors. However, Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans)
    gives "error: expression cannot be used as a function." How can I solve that? Also do I need to use NOTIFY and signal slot mechanism in here? Or once I use Q_PROPERTY and READ, will the value be automatically updated?

    jsulmJ 1 Reply Last reply
    0
    • G Gunkut

      Hello I want to use a variable from .qml file in a .cpp file. I want to do something according to value of (which is in .qml file) isCamRezistans in C++. So I want to put it's value into m_isCamRezistans. For that I use Q_PROPERTY as follows:

      class CAN : public QObject
      {
         Q_OBJECT
      
      public:
         Q_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged)
         Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans)
      
      private:
         quint32 bitrate;
         bool m_isCamRezistans
      

      Q_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged) does not give any errors. However, Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans)
      gives "error: expression cannot be used as a function." How can I solve that? Also do I need to use NOTIFY and signal slot mechanism in here? Or once I use Q_PROPERTY and READ, will the value be automatically updated?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Gunkut said in Q_PROPERTY:

      READ m_isCamRezistans

      READ requires a function not a variable, see https://doc.qt.io/qt-5/properties.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • G Offline
        G Offline
        Gunkut
        wrote on last edited by
        #3

        Okay so is there an easier way to get values of qml variables. Because I will have many variables and writing a function for each of them looks inefficient.

        J.HilkJ jsulmJ 2 Replies Last reply
        0
        • G Gunkut

          Okay so is there an easier way to get values of qml variables. Because I will have many variables and writing a function for each of them looks inefficient.

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

          @Gunkut

          Because I will have many variables and writing a function for each of them looks inefficient

          Assuming you use QtCreator:

          • Right mouse click on Q_PROPERTY
          • Select Refactor
          • Select Generate Missing Q_PROPERTY Members

          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
          • G Gunkut

            Okay so is there an easier way to get values of qml variables. Because I will have many variables and writing a function for each of them looks inefficient.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Gunkut From the documentation I linked: "A READ accessor function is required if no MEMBER variable was specified". So, you don't have to provide READ at all.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply
            3
            • jsulmJ jsulm

              @Gunkut From the documentation I linked: "A READ accessor function is required if no MEMBER variable was specified". So, you don't have to provide READ at all.

              G Offline
              G Offline
              Gunkut
              wrote on last edited by
              #6

              @jsulm I see in that case can I only use MEMBER and not NOTIFY? Would a change in qml variable directly change the member variable? Such as using:

              Q_PROPERTY(QString text MEMBER m_text )
              

              instead of :

              Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
              
              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