[Interest] Qml Loader initial property setter

Jérôme Godbout jerome at bodycad.com
Fri Apr 1 16:05:36 CEST 2016


Yes, but the binding loaded object internal binding are printing error,
since the object is created without the input, then the external binding
are created. They will get eventually correct, but the console will be
filled with error message and I try to avoid that at all cost. Here's a
simple example that will complain about unable to access i and j of
undefined / assign undefined to width and height (this example is a bit
stupid but just a demo to show the problems with more complex model and
object):

// MyComponent.qml
Item
{
 property var model
 width: model.i
 height: model.j
}

// Main.qml
MyModel
{
  id: myModel_
  property int i: 24
  property int j: 12
}

Loader
{
  id: loader_
  source: "MyComponent.qml"
}

Binding
{
  target: loader_.item
  property: 'model'
  value: myModel_
  when: loader_.status == Loader.Ready
}

What I would like is the Loader call QQmlComponent::beginCreate(), then
having a hook to init some property then the Loader can finish the
component QQmlComponent::completeCreate(). This way I was able to make it
work, but having a custom component for that is a bit annoying.

Loader
{
  source: "MyComponent.qml"
  onInitProperty:  // signal emit with component object returned by
beginCreate(),
  {
    object.model = Qt.binding(function(){ return myModel_; });
    ...
  }
}

That seem clean and would avoid the problems to have object into bad state
for a brief moment.

Jerome

On Fri, Apr 1, 2016 at 12:04 AM, Shantanu Tushar <shaan7in at gmail.com> wrote:

> Hi,
>
> Have you tried using the QML Binding Type?
> http://doc.qt.io/qt-5/qml-qtqml-binding.html
>
> On Thu, Mar 24, 2016 at 11:11 PM, Jérôme Godbout <jerome at bodycad.com>
> wrote:
>
>> Hi,
>>
>> I was wondering if I am the only one who found the initial property of a
>> source based Loader Item a bit annoying. inline sourceComponent with
>> Component {} declaration are well ok on the other hand. I'm explaining
>> myself, when using a loader you have 2 choice to set the initial binding on
>> created item:
>>
>> 1- On item changed set the binding
>>
>> *Loader*
>> *{*
>> *  source: externalObj.getSrc() // return something like "myItem.qml"*
>> *  onItemChanged:*
>> *  {*
>> *    if(!item) return;*
>> *    item.myprop = Qt.binding(function(){ return myExternalProperty; })*
>> *  }*
>> *}*
>>
>> 2- use the setSource() function instead of the source directly
>>
>> *Loader*
>> *{*
>> *  property url myDummySrc: externalObj.getSrc()*
>> *  onMyDummySrcChanged:*
>> *  {*
>> *     setSource(myDummySrc, { 'myprop': Qt.binding(function(){ return
>> myExternalProperty; }) });*
>> *  }*
>> *}*
>>
>> This is rather annoying and ugly. Number 1 is also having the problem
>> where *myItem.qml* must be able to handle *myprop *to have a default
>> value or being empty without output a lots of console error. Number 2 work
>> well and is the solution I find myself writing often. I was wondering if a
>> Loader that support a dictionary of initial property when source changed
>> would be nice:
>>
>> *Loader*
>> *{*
>> *  source: externalObj.getSrc()*
>> *  initialProperties: { 'myprop': Qt.binding(function(){ return
>> myExternalProperty; })  }*
>> *}*
>>
>> What do you think?
>> Jerome
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
>
> --
> Shantanu Tushar    (UTC +0530)
> shantanu.io
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160401/2fa3e711/attachment.html>


More information about the Interest mailing list