[Interest] DelegateModel: Dynamic Delegate Model groups

Jérôme Godbout jerome at bodycad.com
Wed Jul 27 15:34:44 CEST 2016


The way you create the object seem to be all fine. It's the container of
multiple obj that may have a problem with it.

When you specify a QQmlListProperty<A> you cannot assign anything else then
A object into it and sometime it may get annoying when you have a list<B>
cannot assign since they are not of the specified A type (even if A is B,
the invert would cause you no trouble).

in your case A is DelegateModelGroup
B is QObject*

I'm guessing you are doing a loop on Compoenent create (coudl use an
instanciator) of the component and try to assign the generated list (which
are QQmlListProperty<QtObject> into QQmlListProperty<DelegateModelGroup>
list, am I right?

DelegateModelGroup instance can be assign to list<QtObject>,
list<DelegateModelGroup>
but not to list<Item>!

Item { id: component property list<Item> a property list<QtObject> b
property list<DelegateModelGroup> c Instantiator { model: 2 onObjectAdded:
{ console.log('added', object); function append_obj(l) {
console.log('before', l); try { var tempo = adaptQQLP(l);
tempo.push(object); console.log('list', tempo); return tempo; } catch(e) {
console.log('!!!ERROR'); console.log(e); return []; } } component.a =
append_obj(component.a); component.b = append_obj(component.b); component.c
= append_obj(component.c); console.log('after a', component.a);
console.log('after b', component.b); console.log('after c', component.c); }
onObjectRemoved: { console.log('removed', object); }
// Switch delegate to see the effect //QtObject {} //Item {}
DelegateModelGroup {} } Component.onCompleted: { console.log('End',
adaptQQLP(a), adaptQQLP(b), adaptQQLP(c)); } }

On Tue, Jul 26, 2016 at 4:07 PM, Jason H <jhihn at gmx.com> wrote:

> Oh, I may see the problem, I created the objects as:
>
> Component {
> 	id: recipientComponent
> 	DelegateModelGroup {}	
> }
>
> var recipientGroup = recipientComponent.createObject(recipientComponent, {includeByDefault: false, name: number});
>
> I don't know how I can more strongly type that. I'd expect Qt to
> internally try to promote it and succeed.
> *Sent:* Tuesday, July 26, 2016 at 1:43 PM
>
> *From:* "Jérôme Godbout" <jerome at bodycad.com>
> *To:* "Jason H" <jhihn at gmx.com>
> *Cc:* "André Somers" <andre at familiesomers.nl>, "Interests Qt" <
> interest at qt-project.org>
> *Subject:* Re: [Interest] DelegateModel: Dynamic Delegate Model groups
> Just to make sure the concat really generate the JS array, try the
> following (It should not give any difference, but just to make sure):
>
> function adaptQList(src)
> {
> var rv = [];
> for(var i = 0; i < src.length; ++i)
> rv.push(src[i]);
> return rv;
> }
>
> But I suspect you have a type mismatch here, since you have a
> QList<QObject*> and a QQmlLisProperty<QQmlDelegateModelGroup>, try with a
> property of type QQmlLisProperty<QObject> and affect the value to see if
> this work (it should). If this work you will need to convert type of have a
> setter that take QQmlLisProperty<QObject> and do the C++ type
> check/conversion to your strongly typed QQmlDelegateModelGroup.
>
> Jerome
>
>
> On Tue, Jul 26, 2016 at 1:33 PM, Jason H <jhihn at gmx.com> wrote:
>>
>>
>> Thanks for the pointer, but when I hacked it in as a p.o.c:
>>
>> groups = [].concat(recipientModelGroups); //Error: Cannot assign QList<QObject*> to QQmlListProperty<QQmlDelegateModelGroup>
>>
>> (groups is a DelegateModel.group property, recipentModelGroups is a js
>> array of DelegateModelGroups). I think I'm trying to go the other way? Or
>> the QQmlListProperty<QQmlDelegateModelGroup> is too tightly typed?
>>
>>
>> *Sent:* Tuesday, July 26, 2016 at 11:49 AM
>> *From:* "Jérôme Godbout" <jerome at bodycad.com>
>> *To:* "Jason H" <jhihn at gmx.com>
>> *Cc:* "André Somers" <andre at familiesomers.nl>, "Interests Qt" <
>> interest at qt-project.org>
>> *Subject:* Re: [Interest] DelegateModel: Dynamic Delegate Model groups
>> QQmlListProperty<> cannot be assign with another QQmlListProperty<> or a
>> QList<>, but it does support javascript Array. I myself made a simple
>> function to convert:
>>
>> function adaptQQLP(list_obj)
>> {
>>    return [].concat(list_obj);
>> }
>> It does iterate on it and create a javascript array which is enough
>>
>> MyQmlObj
>> {
>>    myQQmlListProperty: adaptQQLP(myOtherId_.itsOwnQQmlListProperty)
>> }
>>
>> This is a workaround the QQmlListProperty madness. This and the clear and
>> push everything back for any modification where performance goes down the
>> toilette. Tumb rules avoid those QQmlListProperty as much as possible
>> your software performance will thank you.
>>
>> Jerome
>>
>> On Tue, Jul 26, 2016 at 11:23 AM, Jason H <jhihn at gmx.com> wrote:
>>>
>>> > I'd just write my proxy model in C++ and be done with it.
>>> >
>>> > André
>>> >
>>> >
>>> > Op 26/07/2016 om 15:46 schreef Jason H:
>>> > > I want to have groups corresponding to the equivalent of 'SELECT
>>> DISTINCT x' query. Then I want each item assigned to a group of it's value
>>> of x. For example:
>>> > > 'SELECT DISTINCT x' -> ['a', 'b', 'c']
>>> > > 'SELECT x,y' -> [ ['a', '1'], ['a','2'], ['b','3'], ['c', '4'],
>>> ['c','11'] ]
>>> > >
>>> > > Then I have 3 groups: a has 2, b has 1, c as 2, then I want to set
>>> filerOnGroup to one of the groups. Is there a way to do this?
>>>
>>> DelegateModel.groups is a list<DelegateModelGroup>, but there seems to
>>> be no way to add a group to it.
>>> The DelegateModel.groups property resolves (in QtCreator) to an instance
>>> of a DelegateModelGroup (having .addGroup()) which applies to adding groups
>>> to *model items* and not groups it self.
>>>
>>> When I console.log groups, I get:
>>> {"0":{"objectName":"","count":8,"name":"items","includeByDefault":true},"1":{"objectName":"","count":0,"name":"persistedItems","includeByDefault":false}}
>>>
>>> So I try:
>>>   var recipientGroup =
>>> recipientComponent.createObject(recipientComponent, {includeByDefault:
>>> false, name: number}) // (works)
>>>   groups[groups.length] = recipientGroup; but it doesn't take.
>>> Of I try to assign them all at once: groups = recipientModelGroups;
>>> I get: Error: Cannot assign QList<QObject*> to
>>> QQmlListProperty<QQmlDelegateModelGroup>
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/interest
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160727/4c7494f9/attachment.html>


More information about the Interest mailing list