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

QQmlApplicationEngine and setContextProperty

    XMLWordPrintable

Details

    • User Story
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 5.15.0
    • PySide
    • None
    • Linux/Wayland

    Description

      Hello. I know what can be used PySide with QML. For access to property in Python sourcse needed register context property for QML in Python source, like this:

      ...
      class MyMain(QObject):
       def __init__(self):
        super(MyMain, self).__init__()
      
       @Property(str)
       def myprop(self):
        return 'My Property'
      ...
      mymain = MyMain()
      engine = QQmlApplicationEngine()
      engine.rootContext().setContextProperty('mymain', mymain)
      

      in QML

      ...
       Lable {text: mymain.myprop}
      ...

       

      But i have question. Maybe create context property classes which is an attribute of the class:

      ...
      class SubCl(QObject):
       def __init__(self):
        super(SubCl, self).__init__()
      
       @Property(str)
       def subprop(self):
        return 'Sub Property'
      
      class MyMain(QObject):
       def __init__(self):
        super(MyMain, self).__init__()
        self.sub = SubCl()
      
       @Property(QObject)
       def subcl(self):
        return self.sub
      
       @Property(str)
       def myprop(self):
        return 'My Property'
      ...
      mymain = MyMain()
      engine = QQmlApplicationEngine()
      engine.rootContext().setContextProperty('mymain', mymain)
      # in QML: mymain.subcl.subprop - worked
      engine.rootContext().setContextProperty('submain', mymain.sub)
      # or
      engine.rootContext().setContextProperty('submain', mymain.subcl)
      # in QML: submain.subprop - not worked, why?
      

       

      This is bug or feature?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            weedman Michl Voznesensky
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes