Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1096

Accessing QVariant attributes from QML

    XMLWordPrintable

Details

    • Bug
    • Resolution: Incomplete
    • P2: Important
    • None
    • 5.13.1
    • PySide
    • None
    • Windows

    Description

      I have a @dataclass that contains a list of attributes I want to display in my UI using QML. However, when it tries to display information it will say undefined. I am using PySide2 version 5.13.1.  I have attached example code to reproduce the issue.  Coming from being used to using Qt with C++, I would traditionally do something like Q_DECLARE_METATYPE to make my custom type available as a QVariant but a PySide2 equivalent doesn't seem to exist.
       
      main.py

      import sys
      
      from PySide2.QtCore import QUrl
      from PySide2.QtQml import QQmlApplicationEngine, qmlRegisterType
      from PySide2.QtWidgets import QApplication
      
      from information_item import InformationItem
      
      import qml_rc
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
      
          qmlRegisterType(InformationItem, "InformationItem", 1, 0, "InformationItem")
      
          engine = QQmlApplicationEngine()
          engine.load(QUrl("qrc:/main.qml"))
      
          if not engine.rootObjects():
              sys.exit(-1)
      
          sys.exit(app.exec_())
      

      information.py

      from typing import List
      
      from dataclasses import dataclass, field
      
      
      @dataclass
      class Information:
          first_name: str = ""
          last_name: str = ""
          phone_numbers: List[str] = field(default_factory=list)
      

      information_item.py

      from typing import List
      
      from PySide2.QtCore import Property
      from PySide2.QtQuick import QQuickItem
      
      from information import Information
      
      
      class InformationItem(QQuickItem):
          def __init__(self, parent: QQuickItem = None) -> None:
              super().__init__(parent)
      
              self._information = Information("John", "Doe", ["(123) 456-7890", "(321) 654-0987"])
      
          @Property("QVariant", constant=True)
          def information(self) -> Information:
              print(f"INFORMATION: {self._information.first_name} {self._information.last_name} {self._information.phone_numbers[0]} {self._information.phone_numbers[1]}")
              return self._information
      

      main.qml

      import QtQuick 2.13
      import QtQuick.Controls 2.13
      
      ApplicationWindow {
          width: 500
          height: 500
          visible: true
      
          InformationItem {
              anchors.fill: parent
          }
      }
      

      InformationItem.qml

      import QtQuick 2.13
      
      import InformationItem 1.0
      
      InformationItem {
          id: informationItem
          anchors.fill: parent
      
          Column {
              Text {
                  text: "First Name " + informationItem.information.first_name
              }
      
              Text {
                  text: "Last Name " + informationItem.information.last_name
              }
      
              Text {
                  text: "Phone Number 1 " + informationItem.information.phone_numbers[0]
              }
      
              Text {
                  text: "Phone Number 2 " + informationItem.information.phone_numbers[1]
              }
          }
      
          Component.onCompleted: {
              console.log("informationItem.information: " + informationItem.information)
          }
      }
      

      I build and run my program like so:

      $ pipenv install
      $ pipenv run pyside2-rcc -o qml_rc.py qml.qrc
      $ pipenv run python main.py
      

      Output:

      INFORMATION: John Doe (123) 456-7890 (321) 654-0987
      INFORMATION: John Doe (123) 456-7890 (321) 654-0987
      INFORMATION: John Doe (123) 456-7890 (321) 654-0987
      INFORMATION: John Doe (123) 456-7890 (321) 654-0987
      INFORMATION: John Doe (123) 456-7890 (321) 654-0987
      qml: informationItem.information: QVariant(PySide::PyObjectWrapper, )
      qrc:/InformationItem.qml:19: TypeError: Cannot read property '0' of undefined
      qrc:/InformationItem.qml:23: TypeError: Cannot read property '1' of undefined
      

      Data Class Access.zip

      Attachments

        1. Data Class Access.zip
          2 kB
        2. XCn4m.png
          XCn4m.png
          5 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            fgsdbudwin Drew
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes