当前项目更改时会发出此信号。当前项目由current指定,这将替换先前的当前项目。
尝试一下: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_())