Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Convert nested Qobject into python object
Qt 6.11 is out! See what's new in the release blog

Convert nested Qobject into python object

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Acs Chris
    wrote on last edited by
    #1

    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

    jsulmJ JonBJ 2 Replies Last reply
    0
    • A Acs Chris

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • A Acs Chris

        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

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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.

          A Offline
          A Offline
          Acs Chris
          wrote on last edited by
          #4

          @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

          JonBJ 1 Reply Last reply
          0
          • A Acs Chris

            @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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Acs-Chris
            So this is a question about QML, which you did not mention before?

            A 1 Reply Last reply
            0
            • JonBJ JonB

              @Acs-Chris
              So this is a question about QML, which you did not mention before?

              A Offline
              A Offline
              Acs Chris
              wrote on last edited by
              #6

              @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 like

              source.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,
              )
              
              JonBJ 1 Reply Last reply
              0
              • A Acs Chris

                @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 like

                source.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,
                )
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @Acs-Chris
                I believe you have to do it explicitly as you have done.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved