[Solved] Nesting QtObject in QtQuick
-
Hi,
I was looking at http://qt-project.org/wiki/QmlStyling and got an idea to nest "QtObjects":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-item.html so that I could do something like this:
@// Style.qml
QtObject {
property alias text: text
property alias window: windowQtObject{ id: text property color normal: "black" property color disabled: "gray" } QtObject{ id: window property color background: "white" }
}
// root component
Rectangle {
...
Style { id: style }
...
}// in use
Text {
color: style.text.normal
text: "Hello World"
}@But it turns out that QtObject does not have any children property and thus can't be nested this way. On the other hand you might say, use "Item":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-item.html instead. But Item is more for visual object and I want a lightweight module like QtObject.
So my question; is there any other QtQuick modules I could use? Or do I have to make my own? Perhaps there is a neat trick to do this?
Solution:
[quote author="Tomma" date="1372162774"]You can fix that by using those QtObjects as properties without aliasing.
[/quote]@QtObject {
property QtObject text: QtObject{
property color normal: "black"
property color disabled: "gray"
}
property QtObject window: QtObject{
property color background: "white"
}
}@ -
-That would not work anyway because internal objects of a file you are including are not exposed to the user ("window" and "text" objects are not visible to QML code in your root component or in use).-
EDIT: sorry I've obviously misread your post ;) I somehow missed the "alias" part.
-
[quote author="sierdzio" date="1372161354"]I somehow missed the "alias" part.[/quote]
The alias works like a charm. I can change all the root objects to be Item instead of QtObject, but then I will get a conflict if I have property names like width or height etc. And those are names I might use.
-
What you could do instead is to leverage JSON (quite easy and should work) or attached properties (hardcore, haven't done this myself).
@
QtObject {
property variant text: { "normal": "black", "disabled": "gray" }
// In QtQuick 2.0, better use 'var' instead of 'variant'
}// use it!
Text {
color: style.text.normal
text: "Hello World"
}
@ -
You can fix that by using those QtObjects as properties without aliasing.
@QtObject {
property QtObject text: QtObject{
property color normal: "black"
property color disabled: "gray"
}
property QtObject window: QtObject{
property color background: "white"
}
}@ -
@sierdzio: Thanks, I was trying that as well, but then I had to parse int values and did not get the color helper from Qt Crator when using colors. I might have done something wrong, but I felt it was not the most correct way of doing things.
@Tomma: That actually solves my problem! Thanks! I knew there was a simple solution!