简体   繁体   English

Pyqt5 QTreeWidget CurrentItemChanged信号发送整数作为前一项

[英]Pyqt5 QTreeWidget CurrentItemChanged signal sending integers as previous item

I've got a QTreeWidget item and the signal CurrentItemChanged connected to a method that updates the GUI based on a DB lookup. 我有一个QTreeWidget项目,信号CurrentItemChanged连接到了一种基于数据库查找更新GUI的方法。

The CurrentItemChanged documentation says it will pass QTreeWidgetItems as arguments to the method. CurrentItemChanged文档表示它将QTreeWidgetItems作为参数传递给该方法。 First the current item, then the item that had been selected previously. 首先是当前项目,然后是先前已选择的项目。

I am seeing a QTreeWidgetItem followed by an integer, probably the column of the currently selected item, getting passed instead. 我看到QTreeWidgetItem后面跟一个整数,可能是当前所选项目的列,而是通过了。 This behavior does not seem to be part of anyone's documentation for Pyqt5. 这种行为似乎并不属于任何Pyqt5文档的一部分。

Before I go storing references to the previous Item myself, is there something I am missing? 在我自己存储对上一个项目的引用之前,我缺少什么吗? The code is depressingly simple: 该代码非常简单:

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. 当前项目由current指定,并且它将替换先前的当前项目。

Try it: 试试吧:

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_())

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM