Important: Please read the Qt Code of Conduct -

Strange behavior of Q_PROPERTY wrapped by custom macro in Qt 4

  • Hi there!

    I use Q_PROPERTY macro and it is convenient for me to wrap it by my custom macro. But in this case such property does not work (it does not affect the value, returned by QMetaObject::propertyCount() method). I see this problem in Qt v4.7.4 and does not see in Qt v5.5.1.

    I can demonstrate it with the following example:


    #include <QCoreApplication>
    #include <QObject>
    #include <QMetaObject>
    #include <QMetaProperty>
    #include <QDebug>
    #include "Entity.h"
    int main(int argc, char* argv[])
        QCoreApplication a(argc, argv);
        qDebug() << "Qt version: " << QT_VERSION_STR;
        Entity e;
        const QMetaObject *m = e.metaObject();
        qDebug() << "Properties count:  " << m->propertyCount();
        qDebug() << "Properties offset: " << m->propertyOffset();
        for (int i = m->propertyOffset(); i < m->propertyCount(); ++i)
            const QMetaProperty p = m->property(i);
            qDebug() << << ":" <<;
        return 0;//a.exec();


    #include <QObject>
    #define ENTITY_PROPERTY(type, name) Q_PROPERTY(type name READ get_##name)\
            type get_##name() const { return name; }\
            type name;
    class Entity : public QObject
    	explicit Entity(QObject *parent = 0);
        Q_PROPERTY(int id READ get_id)
        int get_id() const;
        int id;
        ENTITY_PROPERTY(int, value)


    #include "Entity.h"
    Entity::Entity(QObject *parent)
    	: QObject(parent)
    int Entity::get_id() const
      	return id;

    QT += core
    QT -= gui
    CONFIG += c++11
    TARGET = test
    CONFIG += console
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp \
    HEADERS += Entity.h

    You can see in Entity.h file, that "id" and "value" properties are declared by the same way, but the first of them is declared "manually", whereas the second one - via ENTITY_PROPERTY macro.

    When the project is built with Qt v4.7.4 I see the following in program output:

    Qt version:  4.7.4 
    Properties count:   2 
    Properties offset:  1 
    id : QVariant(int, 27640304)

    There is no such problem in Qt v5.5.1:

    Qt version:  5.5.1
    Properties count:   3
    Properties offset:  1
    id : QVariant(int, 0)
    value : QVariant(int, 0)

    Is it possible to avoid this problem in Qt v4.7.4?

  • Moderators

    MOC doesn't expand nested macros in Qt4, but this was fixed in Qt5
    -> see corresponding QTBUG

Log in to reply