Details
Description
When I try to access the data from the Model for a selected item in the tree I get a "Internal C++ object already deleted error" - but as far as I can see I have ensured that I have a current reference to the item data in scope.
The test case below demonstrates the issue. When the user selects the item from the Tree and then clicks the button the following error is reported:
Traceback (most recent call last): File "test_case.py", line 33, in button_clicked self.tree.selectionModel().selection()[0] RuntimeError: Internal C++ object (PySide2.QtCore.QAbstractItemModel) already deleted.
Clearly something is going out of scope but I can not see where.
from PySide2.QtWidgets import (QApplication, QMainWindow, QAbstractItemView, QPushButton, QVBoxLayout, QGroupBox, QTreeWidget, QTreeWidgetItem) class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.tree = QTreeWidget(self) self.button = QPushButton(self, text="Run Tests") self.vbox = QVBoxLayout(self) self.vbox.addWidget(self.tree) self.vbox.addWidget(self.button) self.frame = QGroupBox(self, title="Test &Run") self.frame.setLayout(self.vbox) self.button.clicked.connect(self.button_clicked) self.setCentralWidget(self.frame) self.item = QTreeWidgetItem(self.tree) self.item.setText(0, "list item 1") self.show() def button_clicked(self): data = self.tree.selectionModel().model().data( self.tree.selectionModel().selection()[0] ) if __name__ == "__main__": app = QApplication([]) app.mainWindow = MainWindow() app.exec_()