如何通过单击其item-widget选择QTreeWidgetItem

时间:2014-08-10 22:47:23

标签: python pyqt

此代码创建QTreeWidget,其中QComboBoxQLineEdit设置为item widgets。 如果要执行以下步骤:

  1. 此对话框打开后......

  2. 单击项目(而不是其项目窗口小部件)。项目的背景颜色变为蓝色。

  3. 现在点击另一个项目的任何项目小部件(Combobox或LineEdit)。

  4. 当项目窗口小部件被选中时,组合框被指定的项目变为蓝色。通过单击其项目窗口小部件来选择项目的任何其他尝试都将失败。您必须关闭对话框并再次重新打开它。因为这只发生一次。

    是否可以使此功能持久...所以每次点击一个项目窗口小部件时,选择持有它的项目以及完整的印象,该项目及其项目窗口小部件是相同的。因为它的行为 默认我必须单击项目本身才能将其选中。

    enter image description here

        from PyQt4 import QtCore, QtGui
        app = QtGui.QApplication([])
    
        class Tree(QtGui.QTreeWidget):
            def __init__(self, *args, **kwargs):
                super(Tree, self).__init__()
                for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
                    item=QtGui.QTreeWidgetItem()
                    item.setText(0, each)
                    self.addTopLevelItem(item)
                    self.setItemWidget(item, 1, QtGui.QComboBox())
                    self.setItemWidget(item, 2, QtGui.QLineEdit())
                self.setColumnCount(5)
                self.resize(360,240)
                self.show()
    
        tree=Tree()
        sys.exit(app.exec_())
    

1 个答案:

答案 0 :(得分:2)

当小部件中的项目按下时,必须使用mousePressEvent(再次)实施;

.
.
.
comboBox = QtGui.QComboBox()
lineEdit = QtGui.QLineEdit()
self.setItemWidget(item, 1, comboBox)
self.setItemWidget(item, 2, lineEdit)
# Set new event
comboBox.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = comboBox)
lineEdit.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = lineEdit)
.
.
.

def mousePressEventChild (self, QMouseEvent, currentQTreeWidgetItem = None, child = None):
    if isinstance(currentQTreeWidgetItem, QtGui.QTreeWidgetItem): # <- Check this widget has QTreeWidgetItem.
        self.setCurrentItem(currentQTreeWidgetItem) # <- set current index by QTreeWidgetItem.
    super(child.__class__, child).mousePressEvent(QMouseEvent) # <- Default old mouse press event.

完整示例代码;

import sys
from functools import partial
from PyQt4 import QtCore, QtGui

class Tree(QtGui.QTreeWidget):
    def __init__(self, *args, **kwargs):
        super(Tree, self).__init__()
        for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
            item=QtGui.QTreeWidgetItem([each])
            self.addTopLevelItem(item)
            comboBox = QtGui.QComboBox()
            lineEdit = QtGui.QLineEdit()
            self.setItemWidget(item, 1, comboBox)
            self.setItemWidget(item, 2, lineEdit)
            comboBox.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = comboBox)
            lineEdit.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = lineEdit)
        self.setColumnCount(5)
        self.resize(360,240)
        self.show()

    def mousePressEventChild (self, QMouseEvent, currentQTreeWidgetItem = None, child = None):
        if isinstance(currentQTreeWidgetItem, QtGui.QTreeWidgetItem):
            self.setCurrentItem(currentQTreeWidgetItem)
        super(child.__class__, child).mousePressEvent(QMouseEvent)

app = QtGui.QApplication([])
tree=Tree()
sys.exit(app.exec_())

点击活动参考http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#mousePressEvent


此致