gpt4 book ai didi

python - 如何通过单击其项目小部件来选择 QTreeWidgetItem

转载 作者:行者123 更新时间:2023-12-01 05:04:55 33 4
gpt4 key购买 nike

此代码创建 QTreeWidget,并将 QComboBoxQLineEdit 设置为项目小部件。如果要执行以下步骤:

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

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

  3. 现在单击另一个项目(组合框或行编辑)的任何项目小部件。

当项目小部件被选中时组合框被分配给的项目(变成蓝色)。通过单击项目小部件来选择该项目的任何其他尝试都会失败。您必须关闭对话框并再次重新打开它。因为这种情况只发生一次。

是否可以使此功能持久化...因此,每次单击项目小部件时,持有它的项目也会被选中,并给人留下完整的印象,该项目及其项目小部件是同一件事。由于它的作用是默认情况下,我必须单击该项目本身才能将其选中。

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

最佳答案

当小部件中的项目按下时,必须使用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

<小时/>

问候,

关于python - 如何通过单击其项目小部件来选择 QTreeWidgetItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25233749/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com