Fully understand QML Elements
-
Hello everyone,
I am new QML and I have been using it for projects following examples given in the documentation. But today I really want to fully understand how it works to better code applications and to know all the possibilities offered to me.
To answer that question I would like to take the case of the Item class."Item class":http://doc.qt.digia.com/4.7-snapshot/qml-item.html
One question is : putting Image in Item does mean that the Item is inherited by Image ?
@
Item {
Image {
source: "tile.png"
}
Image {
x: 80
width: 100
height: 100
source: "tile.png"
}
Image {
x: 190
width: 100
height: 100
fillMode: Image.Tile
source: "tile.png"
}
}
@One more thing is : How come we have a Key property ? where does it come from actually as it doesn't appear in the Item class?
@
Item {
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_Left) {
console.log("move left");
event.accepted = true;
}
}
Keys.onSelectPressed: console.log("Selected");
}
@I really need to be able to read any class and do a relevant QML coding and not only examples copies.
-
Ad. 1. No. It means that QML engine will assign the Item as parent to the Image. QML components are not "c++" in that regard. What you define in a QML file is the parent-child structure, not inheritance. This is the same as adding widgets to a QMainWindow in QtWidgets - it's all about meta object system and parenting.
Ad. 2. IIRC, Key is an "attached property":http://qt-project.org/doc/qt-4.8/propertybinding.html#attached-properties. This can be officially labelled as QML Magic.
-
Oh thank you ! You're right, I have been trying to see QML Elements like C++ classes.
Another question could be, how can I know which Element may or may not be parent to another Element ?
And last but not the least, are there others "MAGIC" mechanisms that a beginner can encounter ? -
Hm, I think all elements can be children of all other elements. One possible exception might be the QtObject element, as it lacks rendering, so putting graphical elements into it may produce an error - but to be honest I don't remember what the result of such an action was.
If you want to learn, start a new QML project, and write it from ground up - I find it to be the best way to get to know any language. QML is different in it's "philosophy" than C++ or all other imperative languages, so it takes some time to get used to it fully. It is very impressively powerful, though.
As for the magic... it's all a bit magicky. I can't think of any other mechanism right now, but there sure are lots.
-
Pleasure. Feel free to ask more :)