Qt for Python crashes accessing QObject::property

  • 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):
            # 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)

  • Well I doubt many are going to help you because you have not included an MRE (Minimal Reproducible Example) that one can copy/paste and run and get the same error that you are getting. Without it, it is kind of like going hunting while wearing a blindfold.

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

    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
        engine = QQmlApplicationEngine()

    When you run the program it crashes

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

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

  • @daljit97 that is awesome -- but to reiterate for future questions you might have - including an MRE (Minimal Reproducible Example) which means one can simply copy/paste the MRE and run it to get the same results greatly speeds up getting an answer. If you include an MRE that will not run because it has elements removed that are still referenced that is not technically an MRE because it does not reproduce what you are experiencing.