Convert nested Qobject into python object
-
wrote on 19 Jan 2022, 09:27 last edited by
Hey Guys
is there a way to convert a nested PySide6.QtCore.QObject to a regular python object.
currently I access each individuall proptery manually like
qobject.property(property_name)
thank you
-
Hey Guys
is there a way to convert a nested PySide6.QtCore.QObject to a regular python object.
currently I access each individuall proptery manually like
qobject.property(property_name)
thank you
@Acs-Chris What do you mean by "nested QObject" and why do you need to convert it to a Python object? You can use QObject instances in Python already.
-
Hey Guys
is there a way to convert a nested PySide6.QtCore.QObject to a regular python object.
currently I access each individuall proptery manually like
qobject.property(property_name)
thank you
wrote on 19 Jan 2022, 09:49 last edited by@Acs-Chris said in Convert nested Qobject into python object:
a regular python object
As @jsulm has written. If you know that particular property is, say, a string you can always use
str(qobject.property(property_name))
, and you could wrap that it into a convenience Python function/method for re-use. -
@Acs-Chris What do you mean by "nested QObject" and why do you need to convert it to a Python object? You can use QObject instances in Python already.
wrote on 19 Jan 2022, 10:08 last edited by@jsulm with nested I mean that the Qobject has some props that are also qobject's or a QAbstractListModel
why I will do this let say a have a pseudo Qobject with like this
Qobject { property string foo: "foo" property string bar: "bar" property int mynumber: 12 property Qobject baz: { property string name: "Name 1" property int age: 30 property Qobject bam: { property string hobby: "Tennis" property int days: 10 property QAbstractListModel players: listModel } } }
of course I can grab each indivudal property but I was interested is there maybe a function or method to convert this into a regular python object or dict
-
@jsulm with nested I mean that the Qobject has some props that are also qobject's or a QAbstractListModel
why I will do this let say a have a pseudo Qobject with like this
Qobject { property string foo: "foo" property string bar: "bar" property int mynumber: 12 property Qobject baz: { property string name: "Name 1" property int age: 30 property Qobject bam: { property string hobby: "Tennis" property int days: 10 property QAbstractListModel players: listModel } } }
of course I can grab each indivudal property but I was interested is there maybe a function or method to convert this into a regular python object or dict
wrote on 19 Jan 2022, 10:17 last edited by@Acs-Chris
So this is a question about QML, which you did not mention before? -
@Acs-Chris
So this is a question about QML, which you did not mention before?wrote on 19 Jan 2022, 10:31 last edited by@JonB the object comes from qml, that is true, but I think that is not importend because I am on python side and want to Improve handling the QObject.
Maybe it is more clear to add my code, as shown below I access each prop with the property method.
it was cool if there a way to convert the Qobject to Python Object todo such things likesource.id source.boundingBoxes[0].x source.boundingBoxes[0].y
source = data.property("source") bounding_boxes = source.property("boundingBoxes") boxes = [] for item in range(bounding_boxes.rowCount()): qbox = bounding_boxes.get(item) boxes.append( message_pb2.BoundingBox( id=int(qbox.property("id").toNumber()), name=qbox.property("name").toString(), x=int(qbox.property("x").toNumber()), y=int(qbox.property("y").toNumber()), h=int(qbox.property("h").toNumber()), w=int(qbox.property("w").toNumber()), ) ) change_req = message_pb2.Camera( id=source.property("id").toString(), enabled=source.property("enabled").toBool(), name=source.property("name").toString(), addr=source.property("addr").toString(), path=source.property("path").toString(), username=source.property("username").toString(), password=source.property("password").toString(), transport=source.property("transport").toString(), port=int(source.property("port").toNumber()), http_port=int(source.property("httpPort").toNumber()), rotation=int(source.property("rotation").toNumber()), record=source.property("record").toBool(), motion_detection=source.property("motionDetection").toBool(), md_threshold=int(source.property("mdThreshold").toNumber()), md_min_area=int(source.property("mdMinArea").toNumber()), md_max_area=int(source.property("mdMaxArea").toNumber()), md_sensitivity=int(source.property("mdSensitivity").toNumber()), md_alerttime=int(source.property("mdAlerttime").toNumber()), md_use_max_min_filter=int(source.property("mdUseMaxMinFilter").toBool()), bounding_boxes=boxes, )
-
@JonB the object comes from qml, that is true, but I think that is not importend because I am on python side and want to Improve handling the QObject.
Maybe it is more clear to add my code, as shown below I access each prop with the property method.
it was cool if there a way to convert the Qobject to Python Object todo such things likesource.id source.boundingBoxes[0].x source.boundingBoxes[0].y
source = data.property("source") bounding_boxes = source.property("boundingBoxes") boxes = [] for item in range(bounding_boxes.rowCount()): qbox = bounding_boxes.get(item) boxes.append( message_pb2.BoundingBox( id=int(qbox.property("id").toNumber()), name=qbox.property("name").toString(), x=int(qbox.property("x").toNumber()), y=int(qbox.property("y").toNumber()), h=int(qbox.property("h").toNumber()), w=int(qbox.property("w").toNumber()), ) ) change_req = message_pb2.Camera( id=source.property("id").toString(), enabled=source.property("enabled").toBool(), name=source.property("name").toString(), addr=source.property("addr").toString(), path=source.property("path").toString(), username=source.property("username").toString(), password=source.property("password").toString(), transport=source.property("transport").toString(), port=int(source.property("port").toNumber()), http_port=int(source.property("httpPort").toNumber()), rotation=int(source.property("rotation").toNumber()), record=source.property("record").toBool(), motion_detection=source.property("motionDetection").toBool(), md_threshold=int(source.property("mdThreshold").toNumber()), md_min_area=int(source.property("mdMinArea").toNumber()), md_max_area=int(source.property("mdMaxArea").toNumber()), md_sensitivity=int(source.property("mdSensitivity").toNumber()), md_alerttime=int(source.property("mdAlerttime").toNumber()), md_use_max_min_filter=int(source.property("mdUseMaxMinFilter").toBool()), bounding_boxes=boxes, )
wrote on 19 Jan 2022, 10:44 last edited by@Acs-Chris
I believe you have to do it explicitly as you have done.
1/7