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. Qt for Python crashes accessing QObject::property
Forum Updated to NodeBB v4.3 + New Features

Qt for Python crashes accessing QObject::property

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpyside2python
3 Posts 2 Posters 752 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.
  • D Offline
    D Offline
    daljit97
    wrote on last edited by
    #1

    So I am not sure this is a bug or something but I have spent quite sometime to figure this out but couldn't.
    I have subclassed a QAbstractListModel using PySide2, however when I add an item to my model it crashes.
    I figured out that the problem is in the following block of code:

        def data(self, index: QtCore.QModelIndex, role: int = ...):
            item = self.at(index.row()) # this should be a QObject
            pCount = item.metaObject().propertyCount() # check how many properties this item has
            # let's print the properties name
            for p in range(0, pCount):
                print(item.metaObject().property(p).name())
            # output was "getName" "getAge"
            roleName = self.m_displayRoleName if role is QtCore.Qt.DisplayRole else self.m_roles[role]
            if len(roleName) > 0:
                roleString = str(roleName, encoding='utf-8')
                if role is not self.baseRole():
                    print(roleString) # this prints "getName"
                    return item.property(roleString) # the program CRASHES while executing this line
                return item
    
        def at(self, idx):
            if 0 <= idx < len(self.m_items): # self.m_items is a list 
                return self.m_items[idx]
    

    The function above implements the data method as required by any class which subclasses QAbstractListModel. I have tried to change the line that causes the error to:

    return item.property("blablabla")
    

    And now the crash is gone, but obviously the data is not obtained. Otherwise the program always crashes with the following output:

    Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      daljit97
      wrote on last edited by
      #3

      @denni-0 said in Qt for Python crashes accessing QObject::property:

      Okay unless PySide2 is doing some interesting naming conventions this code will not work because MyModelProvider() is not supplied

      The provider is not necessary to the example so I deleted it. Anyway, the solution was that I was assigning the properties as QObjects while Python expected strings or int.
      So changing @QtCore.Property(QtCore.QObject, constant=True) to @QtCore.Property(str, constant=True) worked.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daljit97
        wrote on last edited by daljit97
        #2

        Yes, perhaps I should have done that earlier. The problem can be reproduced with the code below. I have 3 files
        main.py:

        import sys
        
        from PySide2 import QtCore
        from PySide2.QtWidgets import QApplication
        from PySide2.QtCore import Qt, QCoreApplication, QObject, Slot
        from PySide2.QtQml import QQmlApplicationEngine, QQmlContext
        
        
        
        class MyItem(QObject):
            def __init__(self):
                super(MyItem, self).__init__()
                self.name = "John"
                self.age = 22
        
            @QtCore.Property(QtCore.QObject, constant=True)
            def getName(self):
                return self.name
        
            @QtCore.Property(QtCore.QObject, constant=True)
            def getAge(self):
                return self.age
        
        if __name__ == '__main__':
            app = QApplication(sys.argv)
            item = MyItem()
            print(item.property("getName")) # the program crashes here
            QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
            QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
        
            engine = QQmlApplicationEngine()
            engine.load('qml/main.qml')
            sys.exit(app.exec_())
        

        When you run the program it crashes

        1 Reply Last reply
        0
        • D Offline
          D Offline
          daljit97
          wrote on last edited by
          #3

          @denni-0 said in Qt for Python crashes accessing QObject::property:

          Okay unless PySide2 is doing some interesting naming conventions this code will not work because MyModelProvider() is not supplied

          The provider is not necessary to the example so I deleted it. Anyway, the solution was that I was assigning the properties as QObjects while Python expected strings or int.
          So changing @QtCore.Property(QtCore.QObject, constant=True) to @QtCore.Property(str, constant=True) worked.

          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