Pyqt5 QTreeWidget CurrentItemChanged 信号发送整数作为前一项
Pyqt5 QTreeWidget CurrentItemChanged signal sending integers as previous item
我有一个 QTreeWidget 项目和信号 CurrentItemChanged 连接到一个基于数据库查找更新 GUI 的方法。
CurrentItemChanged 文档说它将 QTreeWidgetItems 作为参数传递给该方法。首先是当前项目,然后是之前选择的项目。
我看到 QTreeWidgetItem 后跟一个整数,可能是当前所选项目的列,而是被传递。这种行为似乎不是任何人的 Pyqt5 文档的一部分。
在我自己存储对上一个项目的引用之前,我是否遗漏了什么?代码简单得令人沮丧:
self.TreeWidget.currentItemChanged.connect(self.update) # signal connect
def update(self, old, new):
# do stuff with old and new, both as as QTreeWidgetItems
This signal is emitted when the current item changes. The current item is specified by current, and this replaces the previous current item.
试一试:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.vlayout = QtWidgets.QVBoxLayout()
# tree widget item
tree_widget_item = QtWidgets.QTreeWidgetItem(['Item 1'])
tree_widget_item.addChild(QtWidgets.QTreeWidgetItem(['Sub item 1']))
tree_widget_item.addChild(QtWidgets.QTreeWidgetItem(['Sub item 2']))
# tree widget
tree_widget = QtWidgets.QTreeWidget(self)
tree_widget.addTopLevelItem(tree_widget_item)
self.vlayout.addWidget(tree_widget)
tree_widget.currentItemChanged.connect(self.current_item_changed)
@QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem, QtWidgets.QTreeWidgetItem)
def current_item_changed(self, current, previous):
print('\ncurrent: {}, \nprevious: {}'.format(current, previous))
application = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Tree widget')
window.resize(250, 180)
window.show()
sys.exit(application.exec_())
我有一个 QTreeWidget 项目和信号 CurrentItemChanged 连接到一个基于数据库查找更新 GUI 的方法。
CurrentItemChanged 文档说它将 QTreeWidgetItems 作为参数传递给该方法。首先是当前项目,然后是之前选择的项目。
我看到 QTreeWidgetItem 后跟一个整数,可能是当前所选项目的列,而是被传递。这种行为似乎不是任何人的 Pyqt5 文档的一部分。
在我自己存储对上一个项目的引用之前,我是否遗漏了什么?代码简单得令人沮丧:
self.TreeWidget.currentItemChanged.connect(self.update) # signal connect
def update(self, old, new):
# do stuff with old and new, both as as QTreeWidgetItems
This signal is emitted when the current item changes. The current item is specified by current, and this replaces the previous current item.
试一试:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.vlayout = QtWidgets.QVBoxLayout()
# tree widget item
tree_widget_item = QtWidgets.QTreeWidgetItem(['Item 1'])
tree_widget_item.addChild(QtWidgets.QTreeWidgetItem(['Sub item 1']))
tree_widget_item.addChild(QtWidgets.QTreeWidgetItem(['Sub item 2']))
# tree widget
tree_widget = QtWidgets.QTreeWidget(self)
tree_widget.addTopLevelItem(tree_widget_item)
self.vlayout.addWidget(tree_widget)
tree_widget.currentItemChanged.connect(self.current_item_changed)
@QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem, QtWidgets.QTreeWidgetItem)
def current_item_changed(self, current, previous):
print('\ncurrent: {}, \nprevious: {}'.format(current, previous))
application = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Tree widget')
window.resize(250, 180)
window.show()
sys.exit(application.exec_())