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. Strange behavior of Q_PROPERTY wrapped by custom macro in Qt 4
Qt 6.11 is out! See what's new in the release blog

Strange behavior of Q_PROPERTY wrapped by custom macro in Qt 4

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 960 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.
  • F Offline
    F Offline
    Fundaev
    wrote on last edited by
    #1

    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:

    main.cpp:

    #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() << p.name() << ":" << e.property(p.name());
        }
        return 0;//a.exec();
    }
    

    Entity.h:

    #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
    {
        Q_OBJECT
    public:
    	explicit Entity(QObject *parent = 0);
    
        Q_PROPERTY(int id READ get_id)
        int get_id() const;
        int id;
    
        ENTITY_PROPERTY(int, value)
    };
    

    Entity.cpp:

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

    test.pro:

    QT += core
    QT -= gui
    
    CONFIG += c++11
    
    TARGET = test
    CONFIG += console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    SOURCES += main.cpp \
               Entity.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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      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