[Interest] QObject-derived default property read-only
Brian Dentino
brian.dentino at gmail.com
Mon Oct 13 18:17:26 CEST 2014
Thanks for the suggestion, Marian.
It turns out the problem was that not all of the classes in my hierarchy were registered with qml. The type within the QQmlListProperty was a subclass of another custom QObject-derived type which I didn't register because it's an abstract class. But registering it as an uncreatable-type did the trick and got rid of the "read-only" errors.
Brian
On Oct 13, 2014, at 2:33 AM, Marian Beermann <public at enkore.de> wrote:
> 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
>>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
More information about the Interest
mailing list