Q_REVISION meaning in Qt Class

Viewed 328

what is Q_REVISION in Qt function body?
I saw this so many times in qt source codes.
I have looked into Qt documentation and i saw this documentations says :

Apply this macro to declarations of member functions to tag them with a revision number in the meta-object system.

what does it means? and where can i use this Q_REVISION

1 Answers

As of now this is only used by QtQml, when you need to use a C++ object in QML you must register it using this syntax.

qmlRegisterType<Foo>("MyModule", 1, 0, "Foo");

By doing so you can then import your module in QML and use the Foo object. This syntax exposes to QML all methods and properties marked as Q_REVISION(0), that as the documentation says, is the default value.

Let's say we have our class Foo that has several revisions.

class Foo : public QObject
{
    Q_OBJECT

    Q_PROPERTY(int normalProperty READ normalProperty)
    Q_PROPERTY(int newProperty READ newProperty REVISION 1)
    Q_PROPERTY(int newerProperty READ newerProperty REVISION 2)

public:
    Foo();
public slots:
    int normalProperty() { return 0; }
    Q_REVISION(1) int newProperty() { return 1; }
    Q_REVISION(2) int newerProperty() { return 2; }
};

By using the previous syntax we'll be able to only use the normalProperty in QML, to use other revision we must do this.

qmlRegisterType<Foo, 1>("MyModule", 1, 1, "Foo");

Notice the difference in the template arguments, the previous one doesn't have the have revision number after the class name. It's also good practice to increment the minor version of the module, that is the third argument of the function. This way you'll be able to use normalProperty and newProperty in QML.

qmlRegisterType<Foo, 2>("MyModule", 1, 2, "Foo");

With this call instead you'll be able to use all three properties, normalProperty, newProperty and newerProperty.

You can find even more information on the QObject documentation.

Great question by the way, I didn't know this macro. I hope we both learned something new. :)

Related