Grouped Property Problem



  • 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;
    };
    


  • @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


  • @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.



  • 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



  • @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.



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



  • 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");
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.