将 QAbstractTableModel 与 QTreeView 一起使用时崩溃 [英] Crashing when using QAbstractTableModel with QTreeView

查看:80
本文介绍了将 QAbstractTableModel 与 QTreeView 一起使用时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为它似乎没有专门为 QTreeView设计"的专用"抽象模型(对于 QListViewQAbstractListModel,对于 QTableView 有 QAbstractTableModel) 并且因为我需要能够显示标题,所以我选择使用 Table 的抽象模型:QAbstractTableModel 和 'QTreeView'.代码运行良好,但如果单击加号,它会立即崩溃.QAbstractTableModel 不应该与QTreeView"一起使用吗?使用什么抽象模型?

Since it appears there is no "dedicated" Abstract Model "designed" purposely for QTreeView (for QListView there is QAbstractListModel and for QTableView there is QAbstractTableModel) and since I need to be able to display the headers I opted to use Table's Abstract model: QAbstractTableModel with 'QTreeView'. The code runs fine but if a plus signed is clicked it crashes immidiately. Shouldn't be QAbstractTableModel used with 'QTreeView'? What Abstract model to use?

import os,sys
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)
elements={'Animals':{1:'Bison',2:'Panther',3:'Elephant'},'Birds':{1:'Duck',2:'Hawk',3:'Pigeon'},'Fish':{1:'Shark',2:'Salmon',3:'Piranha'}}

class Model(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractListModel.__init__(self)
        self.items=[] 
        self.modelDict={}
    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)   
    def columnCount(self, index=QtCore.QModelIndex()):
        return 3
    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)): return QtCore.QVariant()
        if role==QtCore.Qt.DisplayRole:          return QtCore.QVariant(self.items[index.row()])
    def buildItems(self):
        totalItems=self.rowCount()
        for key in self.modelDict:    
            self.beginInsertRows(QtCore.QModelIndex(), totalItems+1, 0)
            self.items.append(key)
            self.endInsertRows()

class TreeView(QtGui.QTreeView):
    def __init__(self):
        super(TreeView, self).__init__()
        self.model= Model()
        self.model.modelDict=elements
        self.model.buildItems()
        self.setModel(self.model)
        self.show()       

window=TreeView()
sys.exit(app.exec_())

推荐答案

您不能将 QAbstractTableModelQTreeView 一起正确使用,因为该类仅适用于 QTableView.您必须改为继承 QAbstractItemModel,(其中QAbstractTableModelQAbstractListModel 都继承),并实现了 index(), parent(), rowCount()columnCount()data(),如 子类化 Qt 手册的部分.对于一个QTreeView来说,parent()特别要注意,因为它告诉QTreeView一个项目是否在顶部级别或者它是否是树中另一项的子项.

You cannot use QAbstractTableModel properly with a QTreeView, as that class is only for QTableView. You must instead inherit QAbstractItemModel, (which is what both QAbstractTableModel and QAbstractListModel inherit), and implement index(), parent(), rowCount(), columnCount(), and data(), as described in the subclassing section of Qt's fine manual. For a QTreeView, parent() in specific is very important to pay attention to, as it tells the QTreeView if an item is at the top level or if it's a child of another item in the tree.

我相信在 Qt 中没有 QAbstractTreeModel 类的主要动机是因为您需要重写所有这些方法来创建一个适当表达的树模型.

I believe the primary motivation for not having a QAbstractTreeModel class in Qt is because you would need to override all of these methods to create a properly expressive tree model, already.

这篇关于将 QAbstractTableModel 与 QTreeView 一起使用时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆