Obtain object type string from QML
-
Hello! How I can obtain type of a object from QML code?
I need this feature in Loader:@Loader {
sourceComponent: myObj.type === "MyObject" ? delegate1 : delegate2
}@ -
The correct form is the following:
@a = (b==0)?1:0;@
but in you example I see 3 '='
In any case you cannot use == for the string.
The correct code is the following (I suppose that myObj.type return a string):
@QString mystring;
mystring(myObj.type);
mystring.compare("MyObject") == 0 ? delegate1 : delegate2;@
in your case will be the following:
@Loader {
sourceComponent: myObj.type.compare("MyObject") == 0 ? delegate1 : delegate2
}
@ -
Thank you for reply!
You may read about tripple equals (===) in docs: "Comparison operators":http://www.c-point.com/javascript_tutorial/jsgrpComparison.htmMain problem is obtaining type information about a object from QML. I don't know how do it! In documentation about QObject ("QObject":http://qt-project.org/doc/qt-5/QObject.html) exists only one property - objectName, but metaObject() is not available under QML.
I use small hack:
In my objects i add new property:
@Q_PROPERTY(QString className READ className CONSTANT)QString className()
{
return metaObject()->className();
}
@And in QML:
@ListView {
model: myModel
delegate: Component {
property bool isObject1: className === "MyObject1"Text { text: isObject1 ? property1 : property2 } } }@
This is ugly, but it is works.
-
bq. In any case you cannot use == for the string.
Why ?
-
bq. You may read about tripple equals (===) in docs: Comparison operators [c-point.com]
you are right! I never stop learning.
I developed the following example:
@import QtQuick 2.2
import QtQuick.Controls 1.1ApplicationWindow {
visible: true
width: 2560
height: 1600
color: "white"Rectangle { width: 200 height: 100 color: "red" Text { id: mytext objectName: "MyTextObject" anchors.centerIn: parent text: "Hello, World!" } MouseArea { anchors.fill: parent onClicked: { parent.color = "blue" mytext.text = mytext.objectName == "MyTextObject" ? "YES" : "NO" } } }
}@
I get the name of the object by objectName method:
@mytext.objectName@
in your case you can use:
@Loader {
sourceComponent: myObj.objectName === "MyObject" ? delegate1 : delegate2
}@but you have to assign to your object objectName = "MyObject"
-
So, this is also possible, but objectName is instance specific, not a type specific. It is more suitable to use objectName for other purposes, IMHO.
-
Please share your code about MyObject1 object just to understand better....
-
I mean that if we using objectName() method that we must set this property to all instances of class in constructor or in other places. For example:
@
MyObject1()
{
// setObjectName("MyObject1");
// or
setObjectName(metaObject()->className());
}
@or as you wrote:
@
Text {
id: mytext
objectName: "MyTextObject"
anchors.centerIn: parent
text: "Hello, World!"
}
@But if we using className wrapper property than we may define this methods in our class declaration. And this is allow to us to making safe inheritance from MyObject1: for subclasses className() return actual className!
@Q_PROPERTY(QString className READ className CONSTANT)QString className()
{
return metaObject()->className();
}@and we can using objectName for other purposes, for example:
@
MyObject1 objA = new MyObject1();
objA.setObjectName("A");MyObject1 objB = new MyObject1();
objB.setObjectName("B");
@This is can be useful for debugging.