1
我正在實現一個gui分析軟件,它使用QTreeWidget和一些自定義的TreeWidgetItem類。樹中的每個項目或多或少都對其自己的記錄負責,以跟蹤附加到其上的數據集和對象。我希望能夠從自定義QTreeWidgetItems的函數內發出信號,但以下代碼導致myTWItem無法轉換爲QObject的錯誤。有沒有辦法以這種方式從QTreeWidgetItem發出信號?替代品從QTreeWidgetItem發射信號子類PyQt/PySide
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class myTWItem(QtWidgets.QTreeWidgetItem):
childAdded = QtCore.pyqtSignal(object)
def addChild(self, child):
super(myTWItem, self).addChild(child)
self.childAdded.emit(child)
app = QtWidgets.QApplication(sys.argv)
tw = QtWidgets.QTreeWidget()
item = myTWItem()
tw.addTopLevelItem(item)
item.setText(0, "James")
child = QtWidgets.QTreeWidgetItem()
child.setText(0, "Braddock")
item.addChild(child)
tw.show()
sys.exit(app.exec_())
輸出:要做到這一點
>>Traceback (most recent call last):
>> File "C:/Coding/Python/dataquick/sandbox/treewidgettest.py", line 22, in <module>
>> item.addChild(child)
>> File "C:/Coding/Python/dataquick/sandbox/treewidgettest.py", line 10, in addChild
>> self.childAdded.emit(child)
>>TypeError: myTWItem cannot be converted to PyQt5.QtCore.QObject in this context
>>
>>Process finished with exit code 1