简体   繁体   English

自定义类型的Qml属性

[英]Qml property of custom type

I have a class defined as follows: 我有一个定义如下的类:

class Set : public QObject {
    Q_OBJECT
    …
};

It's qmRegisterType'd and so is usable within QML. 它是qmRegisterType,因此可以在QML中使用。 I'd like to have in that class property of custom type SetRange, which is defined as follows: 我想在自定义类型SetRange的类属性中具有以下定义:

class SetRange : public QObject {
public:
    SetRange ( QObject *parent = 0 ) : QObject::QObject(parent) { }
    Q_PROPERTY(int lowerBound MEMBER lower_bound WRITE setLowerBound NOTIFY lowerBoundChanged)
    Q_PROPERTY(int length MEMBER length WRITE setLength NOTIFY lengthChanged)
    int lower_bound, length;
    void setLowerBound ( int lower_bound ) {
        if ( lower_bound != this->lower_bound )
            emit lowerBoundChanged(this->lower_bound = lower_bound);
    }
    void setLength ( int length ) {
        if ( length != this->length )
            emit lengthChanged(this->length = length);
    }
signals:
    void lowerBoundChanged ( int lower_bound );
    void lengthChanged ( int length );
};
Q_DECLARE_METATYPE(SetRange)

And the property within Set: 以及Set中的属性:

Q_PROPERTY(SetRange range READ range WRITE setRange NOTIFY rangeChanged)
void setRange ( SetRange const &range );
SetRange range ( ) const;

I expect to use it in QML as follows: 我希望在QML中按如下方式使用它:

Set {
    …
    range: Range {
        lowerBound: …
        length: …
    }
}

Currently I have compile-time errors: use of deleted function: SetRange::SetRange(SetRange&&) What should be a proper way to declare custom type to be able to use property of this type both in C++ class and in QML? 当前,我有编译时错误:使用删除的函数:SetRange :: SetRange(SetRange &&)应该是一种正确的方法来声明自定义类型,以便能够在C ++类和QML中使用此类型的属性? This type is qmlRegisterType'd too. 这种类型也是qmlRegisterType的。

Update: Works well even without Q_DECLARE_METATYPE if property range is declared as SetRange* (pointer to SetRange) and other code is modified accordingly. 更新:如果将属性范围声明为SetRange *(指向SetRange的指针)并且相应地修改了其他代码,则即使没有Q_DECLARE_METATYPE,也可以很好地工作。 So the should QML property of custom type always be a pointer to this type? 因此,自定义类型的QML属性应始终是该类型的指针吗?

If your custom property type SetRange is derived from QObject which is not copyable as pointed out in comment by @Vercetti, then your property range can not use SetRange as its type because function setRange() will not work. 如果您的自定义属性类型SetRange派生自QObject ,而@Object注释中指出,该对象不可复制,则您的属性range不能将SetRange用作其类型,因为函数setRange()将不起作用。

One solution is to use a pointer to SetRange as type for your property range , as you have already suggested: 一种解决方案是使用指向SetRange的指针作为属性range类型,正如您已经建议的那样:

Q_PROPERTY(SetRange * range READ range WRITE setRange NOTIFY rangeChanged)

I have been using this solution in all of my custom property types which are derived from QObject. 我在从QObject派生的所有自定义属性类型中都使用了此解决方案。 This always works, and is like a standard coding pattern for me. 这始终有效,并且对我来说就像是标准编码模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM