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. Grouped Property Problem
Forum Updated to NodeBB v4.3 + New Features

Grouped Property Problem

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.5k 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.
  • M Offline
    M Offline
    maydin
    wrote on last edited by
    #1

    I am following this example: http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#grouped-properties

    When I want to access them I get this error: Invalid grouped property access

    Bataque {
            id: bataque
            model.name: "asd"
            author.name: "abc"
    }
    

    I am doing something wrong in registering types. I tried almost everything but could not find the solution.

    And my code is like this:

    
        qmlRegisterType<Batak>("backend", 1, 0, "Bataque");
    //    qmlRegisterType< Model >();          //>> These does not work also. Different errors.
    //    qRegisterMetaType<Model*>("Model*");
        qmlRegisterUncreatableType<Model>("backend", 1, 0, "Modelito", "something wrong");
    
    #include <QObject>
    #include "model.h"
    
    class Bataque: public QObject
    {
        Q_OBJECT
        Q_PROPERTY(Model* model READ model)
        Q_PROPERTY(Model* author READ author)
        Model m_model;
        Model* m_author;
    
    public:
        explicit Bataque(QObject *parent = nullptr): QObject(parent), m_author(new Model(this))
        {
        }
        Model* model()
        {
            return &m_model;
        }
        Model* author() const
        {
            return m_author;
        }
    };
    
    #include <QObject>
    #include <QString>
    
    class Model : public QObject
    {
        Q_PROPERTY(QString name READ name WRITE setName)
    
    public:
    explicit Model(QObject *parent = nullptr) : QObject(parent) {}
    
    QString name() const
    {
        return m_name;
    }
    
    public slots:
    void setName(QString name)
    {
        m_name = name;
    }
    private:
    QString m_name;
    };
    
    1 Reply Last reply
    0
    • KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by KillerSmath
      #2

      @maydin
      Hi there, welcome to Qt Forum.

      I've checked your code and noticed some "mistakes" or confusion on coding.

      1) You forgot of Q_OBJECT macro on Model.h file.

      class Model : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString name READ name WRITE setName)
      ...
      

      2) You don't necessarily need to define in public slot scope to call the read function of Q_PROPERTY, although this works the same way.

      // Model.h //
      
      public: // public scope
      explicit Model(QObject *parent = nullptr) : QObject(parent) {}
      
      QString name() const
      {
          return m_name;
      }
      
      //public slots:
      void setName(QString name) // defined as public
      {
          m_name = name;
      }
      private:
      QString m_name;
      };
      

      3) You explicitly need to say: "I have an object that can be converted from QVariant to Model"

       // main function
       qmlRegisterType<Model>(); // this line is necessary
       qmlRegisterType<Bataque>("backend", 1, 0, "Bataque");
      

      Bonus:

      • Qt Creator has a shortcut key to Auto Identation of your code - Ctrl + I

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      M 1 Reply Last reply
      2
      • KillerSmathK KillerSmath

        @maydin
        Hi there, welcome to Qt Forum.

        I've checked your code and noticed some "mistakes" or confusion on coding.

        1) You forgot of Q_OBJECT macro on Model.h file.

        class Model : public QObject
        {
            Q_OBJECT
            Q_PROPERTY(QString name READ name WRITE setName)
        ...
        

        2) You don't necessarily need to define in public slot scope to call the read function of Q_PROPERTY, although this works the same way.

        // Model.h //
        
        public: // public scope
        explicit Model(QObject *parent = nullptr) : QObject(parent) {}
        
        QString name() const
        {
            return m_name;
        }
        
        //public slots:
        void setName(QString name) // defined as public
        {
            m_name = name;
        }
        private:
        QString m_name;
        };
        

        3) You explicitly need to say: "I have an object that can be converted from QVariant to Model"

         // main function
         qmlRegisterType<Model>(); // this line is necessary
         qmlRegisterType<Bataque>("backend", 1, 0, "Bataque");
        

        Bonus:

        • Qt Creator has a shortcut key to Auto Identation of your code - Ctrl + I
        M Offline
        M Offline
        maydin
        wrote on last edited by
        #3

        @KillerSmath

        1. I did not forgot Q_OBJECT. In example of Qt Docs there is no Q_OBJECT macro. I tried adding that also but it gave me moc errors.

        2. I just used Alt+Space and clicked "Generate missing property members". Those was done automatically.

        3. Added qmlRegisterType<Model>(); but still same error.

        1 Reply Last reply
        0
        • KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by KillerSmath
          #4

          Okay, we are progressing.

          @maydin said in Grouped Property Problem:

          1. I did not forgot Q_OBJECT. In example of Qt Docs there is no Q_OBJECT macro. I tried adding that also but it gave me moc errors.

          Yes, i noticed that in this example has not Q_OBJECT, but this macro allow some features of Meta-Object.

          Q_OBJECT

          The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

          Q_PROPERTY

          This macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System.

          When i remove the Q_OBJECT macro, i got the below message from Qt Log window:

          0_1531080898093_error.png

          References:
          http://doc.qt.io/qt-5/qobject.html#Q_OBJECT
          http://doc.qt.io/qt-5/qobject.html#Q_PROPERTY

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          M 1 Reply Last reply
          2
          • KillerSmathK KillerSmath

            Okay, we are progressing.

            @maydin said in Grouped Property Problem:

            1. I did not forgot Q_OBJECT. In example of Qt Docs there is no Q_OBJECT macro. I tried adding that also but it gave me moc errors.

            Yes, i noticed that in this example has not Q_OBJECT, but this macro allow some features of Meta-Object.

            Q_OBJECT

            The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

            Q_PROPERTY

            This macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System.

            When i remove the Q_OBJECT macro, i got the below message from Qt Log window:

            0_1531080898093_error.png

            References:
            http://doc.qt.io/qt-5/qobject.html#Q_OBJECT
            http://doc.qt.io/qt-5/qobject.html#Q_PROPERTY

            M Offline
            M Offline
            maydin
            wrote on last edited by
            #5

            @KillerSmath

            Okay, I added Q_OBJECT macro on Model class. And registering codes is like this:

                qmlRegisterType<Bataque>("backend", 1, 0, "Bataque");
                qmlRegisterType< Model >();
            //    qRegisterMetaType<Model*>("Model*");
                qmlRegisterUncreatableType<Model>("backend", 1, 0, "Modelito", "something wrong");
            

            Here the errors i see:

            alt text

            As i said before, if i dont add Q_OBJECT macro i can compile without errors and QML engine see my objects without error. Problem occurs at runtime. If I add that macro i cannot even compile.

            1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              Those are errors caused by the MOC not being called.
              Clean and rebuild your project or run qmake manually.

              1 Reply Last reply
              2
              • M Offline
                M Offline
                maydin
                wrote on last edited by
                #7

                Finally I solved.

                These MOC errors came from qmake not updating when you add a Qt macro somewhere.
                If you are using split screen and make changes at right side of screen, sometimes qmake doesnt update. I often run qmake manually for QML files but didn't know this should be done for C++ files too.

                I used both pointer and normal versions of Model instances in Bataque class. Both of them work in QML. You can register them by using one of following functions.

                // Both of them working individually
                qmlRegisterType< Model >();
                qmlRegisterUncreatableType<Model>("backend", 1, 0, "Modelito", "something wrong");
                
                1 Reply Last reply
                2

                • Login

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