[Qt-qml] no basic property define confuse

罗军 luoj at neusoft.com
Thu Sep 30 08:47:04 CEST 2010


Can I think Item has two types of element: property and inner item(QGraphicsItem)?

Rectangle {
    id: rect
    width: 200; height: 200
    property Text a; // This is property, not children item, so it can't be painted by QML.
    property Text b;

    Text { // inner item
        id: foobara 
        text: "foobara"
    }

    a: foobara  //   Its relationship is reference object or clone object?
    b: Text {         // It's not inner item,  what's it?
        parent: rect 
        y: 20
        text: "foobarb"
    }

    gradient: Grdient { // Here, has gradient parent?
         GradientStop { position: 0.0; color: "lightsteelblue" }
         GradientStop { position: 1.0; color: "blue" }
    }  
}




From: Eduardo Fleury 
Sent: Thursday, September 30, 2010 11:30 AM
To: 罗军 
Cc: qt-qml at trolltech.com 
Subject: Re: [Qt-qml] no basic property define confuse


Yes, this works as well.


  I understand, but I confuse this syntax problem.


Are you confused by the ":" vs "=" syntax?

If that's the question, then assume for a moment that you use ":" in QML (like you did) but use "=" in inline JavaScript blocks (like I did inside onClicked). There's a crucial difference between these since the former is a "binding" and the latter an "assigment", but for now, consider that "parent" is being successfully set in both cases.

Or is the confusion regarding the whole idea of setting the parent, etc?

If so, I'm not sure how familiar you are with standard C++ Qt but a little bit of background on QGraphicsScene and QGraphicsItems (the base class of QML items like rectangle and text) helps.

The idea is that in order to be painted on the screen an item needs to:

1) be created
2) have its "visible" property set to true
3) have a valid geometry, that means a non-zero size and be somewhere useful on the screen.
4) be added to what we call the "scene", which is basically the set of items that are handled by the system that paints things in Qt. In QML this is almost hidden as implementation detail.

When we create an item in QML by default only conditions (1) and (2) are met by default.
In the example I sent, I also set the size, so (3) is OK as well.

However it does not yet belong to a scene, and the way of making that happen in QML is setting its parent. When this is done, the underground machinary of Qt will make sure child knows about parent, parent knows about child and that everyone is in the scene and shown... That's the reason parent needs to be set.

Huh... but what about when we create items inside others, like:

Rectangle {
    Text {
        text: "foobar"
    }
}

In this case you don't set the parent of text and yet, it works. Well, what's happening here is that by default, the inner items that are created inside another item automatically becomes children of the outter one. In this case, QML ensures that Text is a child of Rectangle (sets parent, adds to scene, bla, bla) automatically. Kind of a (very important) syntactic sugar.

Let me know if it makes sense!

BR,
Eduardo


-- 
Eduardo M. Fleury
OpenBossa - INdT
http://eduardofleury.com/
http://www.openbossa.org/
---------------------------------------------------------------------------------------------------
Confidentiality Notice: The information contained in this e-mail and any accompanying attachment(s) 
is intended only for the use of the intended recipient and may be confidential and/or privileged of 
Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader of this communication is 
not the intended recipient, unauthorized use, forwarding, printing,  storing, disclosure or copying 
is strictly prohibited, and may be unlawful.If you have received this communication in error,please 
immediately notify the sender by return e-mail, and delete the original message and all copies from 
your system. Thank you. 
---------------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100930/f888ad9e/attachment.html 


More information about the Qt-qml mailing list