[Interest] QObject-derived default property read-only

Marian Beermann public at enkore.de
Mon Oct 13 11:33:05 CEST 2014


The QQmlListProperty you return from attachments() doesn't have an
AppendFunction I guess? You need all four operations (Count, At, Clear,
Append) defined on a list property that's also a default property.

The "backing store" is usually owned by the object the property belongs
to, which is the most convenient solution, as it must remain valid as
long as the property can be accessed.

On 13.10.2014 03:33, Brian Dentino wrote:
> I'm working on a qml plugin in which I register a QObject-derived type
> for use within qml. I've set the default property on this item to be a
> QQmlListProperty<(other qobject derived type)>, and I've setup a READ
> function for this property. For some reason the qml engine will not load
> my application, complaining that this default property is read-only. 
> 
> I understand you typically have to define a WRITE function in order to
> make properties writeable from qml, but the example that comes with Qt
> regarding default properties does not do this, and it compiles fine:
> http://qt-project.org/doc/qt-5/qtqml-referenceexamples-default-example.html
> 
> Here's the class definition I'm using:
> 
> class DefaultContent : public QObject
> 
> {
> 
>     Q_OBJECT
> 
> 
>     Q_PROPERTY(QQmlListProperty<ShareableItem> attachments READ attachments)
> 
>     Q_CLASSINFO("DefaultProperty", "attachments")
> 
> 
> public:
> 
>     explicit DefaultContent(QObject* parent = 0);
> 
> 
>     QQmlListProperty<ShareableItem> attachments();
> 
> 
> signals:
> 
> 
> public slots:
> 
> 
> private:
> 
>     QList<ShareableItem*> m_attachments;
> 
> 
> };
> 
> 
> And the qml...
> 
> *import* QtQuick.2.2
> *import* QtQuick.Window 2.1
> *import* Qtino.SharingKit 1.0 //This is my plugin that registers
> 'DefaultContent'
> 
> Window {
> 
>     DefaultContent {
> 
>         id: defaultContentItem
> 
>         ImageItem { source: rootScreenGrab.url }
> 
>     }
> 
> }
> 
> 
> The error I get is "Invalid property assignment: "attachments" is a read-only property"
> 
> 
> Am I doing something obviously wrong here? If I absolutely must have a
> WRITE method on this property, how would that work with a
> QQmlListProperty type if the backing model is a QList? Would the list be
> owned by js or cpp? And how is that the provided Qt example can work
> without a WRITE method?
> 
> Any help would be much appreciated,
> Brian
> 
> 
> 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
> 




More information about the Interest mailing list