May 19, 2011

Schneidi Schneidi
Lab Rat
86 posts

Loader child element

 

Hi,

I try to access a loader and assign a source file dynamically using JavaScript. That works fine so far.
But now I need to set some properties of the item I loaded to the loader element.

The problem is I don’t know how to access the element that was instantiated by the loader.
Is there some kind of child or whatever property that I can refer to.

Thanks for help guys

5 replies

May 19, 2011

Schneidi Schneidi
Lab Rat
86 posts

Oh no thats embarrassing.

A simple case of reading the docs and everything is fine.

I didn’t read the Loader docs properly.
If I had I had seen the Item property which holds the instance of the loaded item.

Sorry for that guys.

May 19, 2011

Schneidi Schneidi
Lab Rat
86 posts

Oh boy…

Ok another problem occurred.

I have the following procedure.

The qml item which should be instantiated.

  1. import QtQuick 1.0
  2. import Product 0.1
  3.  
  4.  
  5. Item{
  6.  id:detail_screen
  7.     anchors.fill: parent
  8.  
  9.     property alias product: m_product
  10.  
  11.  Text {
  12.   id: name
  13.   anchors.fill: parent
  14.   text: m_product.productName
  15.  }
  16.  
  17.  Product{
  18.   id: m_product
  19.   productName: "foo"
  20.  
  21.     }
  22.  
  23.     Component.onCompleted:
  24.   console.log(product.productName);
  25.  
  26. }

and the JavaScript function which do that for me.

  1. function loadDetailScreen(source,product)
  2. {
  3.  console.log(source);
  4.  select_loader.source = source; //load the qml file shown above
  5.  if(select_loader.status == Loader.Ready)
  6.   {
  7.          select_loader.item.product.productName = "bar";
  8.   }
  9.  console.log(select_loader.item.product.productName); //that actually shows "bar"
  10. }

When the loadDetailScreen() function is called the loader instantiates the qml item
which initially has a productName “foo”. But I wanna set this productName after loading to “bar”
This works pretty well so far. But on screen appears still “foo” as text.

I tried to set a string property which takes the productName and this worked, but I wanna store these informations directly in the product child item.

It seems like that the change on the productName property won’t be synchronized with the text, which has to display it.

What could be the problem here ?
Why does the item show still “foo” although I set it to “bar” after loading ?

Any ideas ?

May 20, 2011

blam blam
Lab Rat
57 posts

I tried running your code as a standalone example and it worked fine – the Text item does indeed update to display “foo” instead of “bar”. Perhaps there is something else in your code that is changing this property after loadDetailScreen() has finished?

May 20, 2011

Schneidi Schneidi
Lab Rat
86 posts

Hi blam

the text item is actually supposed to display “bar” after loadDetailScreen() has finished.
“foo” is the initial value for the text and should be updated to “bar”. Which doesn’t seem to happen.

May 23, 2011

blam blam
Lab Rat
57 posts

Sorry, my mistake – I meant to write, it does indeed change to “bar” instead of “foo”.

This is the code that I’m trying:

  • SomeItem.qml
    1. import QtQuick 1.0
    2.  
    3. Item {
    4.     property alias product: m_product
    5.  
    6.     Text { text: m_product.productName }
    7.  
    8.     Item {
    9.         id: m_product
    10.         property string productName: "foo"
    11.  
    12.     }
    13. }
  • main.qml
    1. import QtQuick 1.0
    2.  
    3. Item {
    4.     width: 400; height: 400
    5.  
    6.     Loader { id: select_loader }
    7.  
    8.     Component.onCompleted: loadDetailScreen("SomeItem.qml")
    9.  
    10.     function loadDetailScreen(source)
    11.     {
    12.         console.log(source);
    13.         select_loader.source = source; //load the qml file shown above
    14.         if(select_loader.status == Loader.Ready)
    15.         {
    16.             select_loader.item.product.productName = "bar";
    17.         }
    18.         console.log(select_loader.item.product.productName); //that actually shows "bar"
    19.     }
    20.  
    21. }
 
  ‹‹ undefined reference to ’vtable for myClass’      WebView Clear history ››

You must log in to post a reply. Not a member yet? Register here!