gpt4 book ai didi

qt - 如何实现基于树的 QComboBox

转载 作者:行者123 更新时间:2023-12-04 13:16:55 27 4
gpt4 key购买 nike

我如何实现 QComboBox允许您从树结构中进行选择,类似于 QTreeView ?

最佳答案

我在 developer.nokia.com( Part 1Part 2 )上使用两部分配方想出了以下类( TreeComboBox ):

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class TreeComboBox(QComboBox):
def __init__(self, *args):
super().__init__(*args)

self.__skip_next_hide = False

tree_view = QTreeView(self)
tree_view.setFrameShape(QFrame.NoFrame)
tree_view.setEditTriggers(tree_view.NoEditTriggers)
tree_view.setAlternatingRowColors(True)
tree_view.setSelectionBehavior(tree_view.SelectRows)
tree_view.setWordWrap(True)
tree_view.setAllColumnsShowFocus(True)
self.setView(tree_view)

self.view().viewport().installEventFilter(self)

def showPopup(self):
self.setRootModelIndex(QModelIndex())
super().showPopup()

def hidePopup(self):
self.setRootModelIndex(self.view().currentIndex().parent())
self.setCurrentIndex(self.view().currentIndex().row())
if self.__skip_next_hide:
self.__skip_next_hide = False
else:
super().hidePopup()

def selectIndex(self, index):
self.setRootModelIndex(index.parent())
self.setCurrentIndex(index.row())

def eventFilter(self, object, event):
if event.type() == QEvent.MouseButtonPress and object is self.view().viewport():
index = self.view().indexAt(event.pos())
self.__skip_next_hide = not self.view().visualRect(index).contains(event.pos())
return False


app = QApplication([])

combo = TreeComboBox()
combo.resize(200, 30)

parent_item = QStandardItem('Item 1')
parent_item.appendRow([QStandardItem('Child'), QStandardItem('Yesterday')])
model = QStandardItemModel()
model.appendRow([parent_item, QStandardItem('Today')])
model.appendRow([QStandardItem('Item 2'), QStandardItem('Today')])
model.setHeaderData(0, Qt.Horizontal, 'Name', Qt.DisplayRole)
model.setHeaderData(1, Qt.Horizontal, 'Date', Qt.DisplayRole)
combo.setModel(model)

combo.show()
app.exec_()

关于qt - 如何实现基于树的 QComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27172160/

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