gpt4 book ai didi

python - 如何将变化的数据延迟加载到 QColumnView 中(使用 PyQt)?

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:53 27 4
gpt4 key购买 nike

我有一个要填充的 QColumnView 的分层数据源。数据源使用 REST 接口(interface)从服务器加载数据。

假设层次结构如下所示:

Car_Manufacturer -> Car_Type -> Specific_Model -> Motor_Type

我必须使用 QColumnView 来显示它(因为这是客户要求)。行为应该是这样的:

当程序启动时,它会从服务器加载 Car_Manufacturer。单击其中一个 Car_Manufacturer 项目时,所选 Car_ManufacturerCar_Type 项目将从服务器加载并显示在新列中。再次单击 Car_Manufacturer 时,必须再次从服务器获取数据并且必须更新该列。单击 Car_Type 时,必须从服务器查询并加载此 Car_ManufacturerCar_typeSpecific_Model 项进入一个新的专栏...等等。

数据源有这个api:

datasource.get_manufacturers(hierarchy)  # hierarchy = []
datasource.get_car_type(hierarchy) # hierarchy = [manufacturer, ]
datasource.get_specific_model(hierarchy) # hierarchy = [manufacturer, car_type]
datasource.get_motor_type(hierarchy) # hierarchy = [manufacturer, car_type, specific_model ]

其中层次结构中的每个元素都是项目的字符串键表示。当一个项目被点击时,它必须通过当前项目的层次结构通知 Controller 。

当使用数据源单击项目时,如何让 QColumnView 更新该项目的子项?添加或删除新的层次结构层时,这如何保持灵 active ?

最佳答案

这是一个实现自定义 DirModel 的示例。方法 _create_children 被延迟调用并且应该返回一个实现 AbstractTreeItem 的实例列表。

import sys
import os
import abc
from PyQt4.QtCore import QAbstractItemModel, QModelIndex, Qt, QVariant
from PyQt4.QtGui import QColumnView, QApplication


class TreeModel(QAbstractItemModel):

def __init__(self, root, parent=None):
super(TreeModel, self).__init__(parent)
self._root_item = root
self._header = self._root_item.header()

def columnCount(self, parent=None):
if parent and parent.isValid():
return parent.internalPointer().column_count()
else:
return len(self._header)

def data(self, index, role):
if not index.isValid():
return QVariant()
item = index.internalPointer()
if role == Qt.DisplayRole:
return item.data(index.column())
if role == Qt.UserRole:
if item:
return item.person
return QVariant()

def headerData(self, column, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
try:
return QVariant(self._header[column])
except IndexError:
pass
return QVariant()

def index(self, row, column, parent):
if not self.hasIndex(row, column, parent):
return QModelIndex()
if not parent.isValid():
parent_item = self._root_item
else:
parent_item = parent.internalPointer()
child_item = parent_item.child_at(row)
if child_item:
return self.createIndex(row, column, child_item)
else:
return QModelIndex()

def parent(self, index):
if not index.isValid():
return QModelIndex()
child_item = index.internalPointer()
if not child_item:
return QModelIndex()
parent_item = child_item.parent()
if parent_item == self._root_item:
return QModelIndex()
return self.createIndex(parent_item.row(), 0, parent_item)

def rowCount(self, parent=QModelIndex()):
if parent.column() > 0:
return 0
if not parent.isValid():
parent_item = self._root_item
else:
parent_item = parent.internalPointer()
return parent_item.child_count()



class AbstractTreeItem(object):

__metaclass__ = abc.ABCMeta

def __init__(self, parent=None):
self._children = None
self._parent = parent

@abc.abstractmethod
def header(self):
#return ["name"]
raise NotImplementedError(self.header)

@abc.abstractmethod
def column_count(self):
#return 1
raise NotImplementedError(self.column_count)

def parent(self):
return self._parent

@abc.abstractmethod
def _create_children(self):
# subclass this method
return []

def row(self):
if self._parent:
return self._parent._children.index(self)
return 0

@property
def children(self):
if self._children is None:
self._children = self._create_children()
return self._children

def child_at(self, row):
return self.children[row]

@abc.abstractmethod
def data(self, column):
#return ""
raise NotImplementedError(self.data)

def child_count(self):
count = len(self.children)
return count



class DirPathModel(AbstractTreeItem):

def __init__(self, root="/", parent=None):
super(DirPathModel, self).__init__(parent)
self._root = root

def _create_children(self):
print "walking into", self._root
children = []
try:
entries = os.listdir(self._root)
except OSError:
# no permission etc
entries = []
for name in entries:
fn = os.path.join(self._root, name)
if os.path.isdir(fn):
children.append(self.__class__(fn, self))
return children

def data(self, column):
#assert column == 0
return os.path.basename(self._root)

def header(self):
return ["name"]

def column_count(self):
return 1


def main():
app = QApplication(sys.argv)
view = QColumnView()
view.setWindowTitle("Dynamic Column view test")
view.resize(1024, 768)
root = DirPathModel("/")
model = TreeModel(root)
view.setModel(model)
view.show()
return app.exec_()


if __name__ == "__main__":
sys.exit(main() or 0)

关于python - 如何将变化的数据延迟加载到 QColumnView 中(使用 PyQt)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353458/

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