gpt4 book ai didi

python - 具有多个 header 的 QStandardItemModel - 将 QTreeView header 与模型分离

转载 作者:行者123 更新时间:2023-12-01 01:58:27 25 4
gpt4 key购买 nike

我有一个 QStandardItemModel,需要在不同 View 中使用不同的水平标题标签。

我无法找到有关如何以这种方式将 QTreeView 的 header 与模型分离的信息。这可能吗?

请参阅下面的简单代码示例。

<小时/>

在此示例中 - 我们有 ItemTypeA,它是我们的顶级项目,其水平轴表示属性“Name | Interpolaton | Normalize”。

ItemTypeA (ItemTypeB) 的子项在水平轴上具有表示“Name | Multiply”的项目

TreeViewA 显示所有内容,TreeViewB 仅显示所选顶级项的子项(本例中未实现选择连接)。

from PySide.QtGui import *
from PySide.QtCore import *

class MyModel(QStandardItemModel):

def __init__(self):
super(MyModel, self).__init__()
iroot = self.invisibleRootItem()

self.setHorizontalHeaderLabels(['Name', 'Interpolation', 'Normalize'])

def newTopLevelItem(self, name = 'myTopLevelItem'):
item = ItemTypeA(name)
root_item = self.invisibleRootItem()
root_item.appendRow([item]+item.settingItems())
return item


class ItemTypeA(QStandardItem):
def __init__(self, *args, **kwargs):
super(ItemTypeA, self).__init__(*args, **kwargs)

self.s_interpolation =QStandardItem('0')
self.s_normalize = QStandardItem('False')

def settingItems(self):
return [
self.s_interpolation,
self.s_normalize
]

def newChildItem(self, name = 'newChildItem'):
childItem = ItemTypeB( name )
self.appendRow( [childItem]+childItem.settingItems() )


class ItemTypeB(QStandardItem):
def __init__(self, *args, **kwargs):
super(ItemTypeB, self).__init__(*args, **kwargs)

self.s_multiply = QStandardItem('1.0')

def settingItems(self):
return [
self.s_multiply,
]


class TreeViewA(QTreeView):
'''
THIS VIEW Needs the Headers:
Item Name | Interpolation | Normalize
'''
def __init__(self):
super(TreeViewA, self).__init__()
model = MyModel()
self.setModel(model)

newItem = model.newTopLevelItem()
newItem.newChildItem()

class TreeViewB(QTreeView):
'''
THIS VIEW Needs the Headers:
Item Name | Multiply
'''
def __init__(self, sourceView):
super(TreeViewB, self).__init__()
model = sourceView.model()
self.setModel(model)
self.setRootIndex(model.index(0,0))


class MyWidget(QWidget):

def __init__(self):
super(MyWidget, self).__init__()
layout = QHBoxLayout()
viewA = TreeViewA()
viewB = TreeViewB(viewA)
layout.addWidget(viewA)
layout.addWidget(viewB)
self.setLayout(layout)

widget = MyWidget()
widget.show()

想法?

最佳答案

这些情况的解决方案是使用 QIdentityProxyModel,但 PySide 和 PySide 中不存在此类,因此我们必须使用类似的类(例如 QSortFilterProxyModel)并覆盖 headerData 方法。

...

class HeaderProxyModel(QSortFilterProxyModel):
def __init__(self, *args, **kwargs):
QSortFilterProxyModel.__init__(self, *args, **kwargs)
self.labels = []
def setHeaderLabels(self, labels):
self.labels = labels
def headerData(self, section,orientation, role = Qt.DisplayRole):
if orientation == Qt.Horizontal and 0 <= section < self.columnCount() and role==Qt.DisplayRole and section < len(self.labels) :
return self.labels[section]
return QSortFilterProxyModel.headerData(self, section, orientation, role)

class TreeViewB(QTreeView):
'''
THIS VIEW Needs the Headers:
Item Name | Multiply
'''
def __init__(self, sourceView):
super(TreeViewB, self).__init__()
model = sourceView.model()
proxy = HeaderProxyModel()
proxy.setSourceModel(model)
proxy.setHeaderLabels(["Name", "Multiply"])
self.setModel(proxy)
self.setRootIndex(proxy.index(0,0))
...

enter image description here

关于python - 具有多个 header 的 QStandardItemModel - 将 QTreeView header 与模型分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926599/

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