• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

QTreeView的QAbstractItemModel:我做错了什么?(QAbstractItemModel for QTreeView: ...

原作者: [db:作者] 来自: 网络 收藏 邀请

QTreeView的QAbstractItemModel:我做错了什么?(QAbstractItemModel for QTreeView: what an I doing wrong?)

我想创建一个QAbstractItemModel的子类,以便在QTreeView使用它。 起初,我决定制作一个没有支持任何层次结构的简约模型。 它应该为2×3表提供简单的文本数据(只读一个)。

唉,它没有崩溃,但没有显示任何数据:只有几个列标题(1,2)。 不能有人告诉我,我做错了什么?

import os, sys

from PyQt5 import QtWidgets, QtCore, QtGui, QtSvg

from PyQt5.QtCore import QPointF, QRectF
from PyQt5.QtWidgets import QGraphicsItem


from gui.graphic_items import *



class MegaModel(QtCore.QAbstractItemModel):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._root_item = 7.40
        pass

    def columnCount(self, parent_index):
        if parent_index.isValid():
            return 0
        else:
            return 2

    def rowCount(self, parent_index):
        if parent_index.isValid():
            return 0
        else:
            return 3

    def data(self, index, role):
        if index.isValid():
            return QtCore.QVariant("Oy vey!")
        else:
            return None

    def index(self, row, column, parent=QtCore.QModelIndex()):
        print("index", row, column, parent)

        if parent.isValid():
            print("valid")
            return parent.internalPointer()
        else:
            print("invalid")
            return self.createIndex(row, column, self._root_item)

    def parent(self, index):
        return QtCore.QModelIndex()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)


    mega_view = QtWidgets.QTreeView()

    mega_model = MegaModel()

    mega_view.setModel(mega_model)

    mega_view.show()

    sys.exit(app.exec_())

I want to create a subclass a QAbstractItemModel to use it in QTreeView. At first, I decided to make a minimalistic model without any hierarchy supported. It should give a 2-by-3 table with simple text data (read-only one).

Alas, It does not crash but does not show any data: only a few column headers (1, 2). Cannot someone tell me, what am I doing wrong?

import os, sys

from PyQt5 import QtWidgets, QtCore, QtGui, QtSvg

from PyQt5.QtCore import QPointF, QRectF
from PyQt5.QtWidgets import QGraphicsItem


from gui.graphic_items import *



class MegaModel(QtCore.QAbstractItemModel):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._root_item = 7.40
        pass

    def columnCount(self, parent_index):
        if parent_index.isValid():
            return 0
        else:
            return 2

    def rowCount(self, parent_index):
        if parent_index.isValid():
            return 0
        else:
            return 3

    def data(self, index, role):
        if index.isValid():
            return QtCore.QVariant("Oy vey!")
        else:
            return None

    def index(self, row, column, parent=QtCore.QModelIndex()):
        print("index", row, column, parent)

        if parent.isValid():
            print("valid")
            return parent.internalPointer()
        else:
            print("invalid")
            return self.createIndex(row, column, self._root_item)

    def parent(self, index):
        return QtCore.QModelIndex()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)


    mega_view = QtWidgets.QTreeView()

    mega_model = MegaModel()

    mega_view.setModel(mega_model)

    mega_view.show()

    sys.exit(app.exec_())

原文:https://stackoverflow.com/questions/42553972
更新时间:2022-03-21 13:03

最满意答案

两个错误:

  1. 您不应该从index方法返回parent.internalPointer() - 此方法不返回模型的存储值,它只是将索引返回到模型项
  2. data方法中,您为所有角色返回QtCore.QVariant("Oy vey!") 。 你应该只为QtCore.Qt.DisplayRole返回这个,对于其他角色,可以返回None

    def data(self, index, role):
        if not index.isValid():
            return None
        if role == QtCore.Qt.DisplayRole:
            return index.internalPointer()
        else:
            return None
    

Two mistakes:

  1. You shouldn't return parent.internalPointer() from index method - this method does not return the model's stored values, it is meant to only return indexes to model items
  2. In data method you return QtCore.QVariant("Oy vey!") for all roles. You should really only return this for QtCore.Qt.DisplayRole, for other roles it is ok to return None:

    def data(self, index, role):
        if not index.isValid():
            return None
        if role == QtCore.Qt.DisplayRole:
            return index.internalPointer()
        else:
            return None
    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap