Must Q_PROPERTYs with user-defined C++ types be of pointer type?



  • Consider the following simple example:

    namespace MyNamespace {
    class MyPoint {
        Q_OBJECT
        Q_PROPERTY(float x READ CONSTANT);
        ...
    };
    class Circle {
        Q_OBJECT
        Q_PROPERTY(MyNamespace::MyPoint centre READ centre NOTIFY centreChanged);
        ...
    };
    }
    Q_DECLARE_METATYPE(MyPoint);
    
    

    An instance of Circle is set as a context property for some qml so it can be used:

    Rectangle {
        x: circleProperty.centre.x
        y: circleProperty.centre.y
        ...
    }
    

    MyPoint is registered using qmlRegisterType<MyPoint>(...). However, this function only calls qRegisterMetaType for MyPoint* and QDeclarativeListProperty<MyPoint> so when I run my app, I get qml errors saying it doesn't know about the MyNamespace::MyPoint type.

    The errors go away if I explicitly use qRegisterMetaType<MyPoint>("MyNamespace::MyPoint"); although now QtQuick won't read through the circleProperty.centre property access to get x. If I print circleProperty.centre it says QVariant(MyNamespace::MyPoint) which is fine but circleProperty.centre.x is undefined.

    Given the behaviour of qmlRegisterType and the errors I see if I work around the behavior, it seems like what I'm trying to do with properties of C++ types without using pointers is not supported. Is that so?


Log in to reply
 

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