Solved 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 aQAbstractListModel
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 subclassesQAbstractListModel
. 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)
-
@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. -
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
-
@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.