Reputation: 457
I have a class defined as follows:
class Set : public QObject {
Q_OBJECT
…
};
It's qmRegisterType'd and so is usable within QML. I'd like to have in that class property of custom type SetRange, which is defined as follows:
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:
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:
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? This type is qmlRegisterType'd too.
Update: Works well even without Q_DECLARE_METATYPE if property range is declared as SetRange* (pointer to SetRange) and other code is modified accordingly. So the should QML property of custom type always be a pointer to this type?
Upvotes: 1
Views: 1848
Reputation: 1444
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.
One solution is to use a pointer to SetRange
as type for your property range
, as you have already suggested:
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. This always works, and is like a standard coding pattern for me.
Upvotes: 1