了解QAbstractTableModel中的MVC [英] Understanding MVC in a QAbstractTableModel

查看:199
本文介绍了了解QAbstractTableModel中的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些由我自己的类表示的数据;修正想法,我举一个例子.

I have some data which are represented by a class of my own ; to fix the ideas I give an example.

class MyOwnModel():
  def __init__(self, name="", number=0):
    self.name = name
    self.number = number

然后我有一个要在QTableView中表示的此类实例的列表.

I then have a list of such instances, that I want to represent in a QTableView.

li = [MyOwnModel("a", 1), MyOwnModel("b", 2)]

然后我看到了两种从中制作QTableView的策略:

Then I see two strategies to make a QTableView from that :

  1. 更改MyOwnModel,以使其成为QAbstractTableModel
  2. 的子类
  3. 构建一个新的QAbstractTableModel,它模仿MyOwnModel,其属性例如是两个QString,并将dataChanged信号连接到更新MyOwnModel实例的函数
  1. change MyOwnModel so that it subclasses QAbstractTableModel
  2. build a new QAbstractTableModel which mimics MyOwnModel in a way that its attributes are for instance two QString and connect the dataChanged signal to a function which updates the instance of MyOwnModel

我对其中的任何一个都不完全满意,但是目前我还没有其他想法.

I am not completely satisfied with any of these, but I have no other idea for the moment.

哪个问题最适合我的问题? (我在实践中有一个更复杂的类,但我想使用相同的框架)

Which one is the most suitable to my problem ? (I have a more complex class in practice but I would like to use the same framework)

推荐答案

如注释中所述,您的模型就是对象列表.您应该使用QAbstractTableModel子类来使用此列表.

As stated in the comment, your model is your list of object. You should subclass QAbstractTableModel to use this list.

这是我的代码段:

import sys
import signal
import PyQt4.QtCore as PCore
import PyQt4.QtGui as PGui

class OneRow(PCore.QObject):
    def __init__(self):
        self.column0="text in column 0"
        self.column1="text in column 1"

class TableModel(PCore.QAbstractTableModel):
    def __init__(self):
        super(TableModel,self).__init__()
        self.myList=[]

    def addRow(self,rowObject):
        row=len(self.myList)
        self.beginInsertRows(PCore.QModelIndex(),row,row)
        self.myList.append(rowObject)
        self.endInsertRows()

    #number of row
    def rowCount(self,QModelIndex):
        return len(self.myList)

    #number of columns
    def columnCount(self,QModelIndex):
        return 2

    #Define what do you print in the cells
    def data(self,index,role):
        row=index.row()
        col=index.column()
        if role==PCore.Qt.DisplayRole:
            if col==0:
                return str( self.myList[row].column0)
            if col==1:
                return str( self.myList[row].column1)

    #Rename the columns
    def headerData(self,section,orientation,role):
        if role==PCore.Qt.DisplayRole:
            if orientation==PCore.Qt.Horizontal:
                if section==0:
                    return str("Column 1")
                elif section==1:
                    return str("Column 2")

if __name__=='__main__':
    PGui.QApplication.setStyle("plastique")
    app=PGui.QApplication(sys.argv)

    #Model
    model=TableModel()
    model.addRow(OneRow())
    model.addRow(OneRow())

    #View
    win=PGui.QTableView()
    win.setModel(model)

    #to be able to close wth ctrl+c
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    #to avoid warning when closing
    win.setAttribute(PCore.Qt.WA_DeleteOnClose)

    win.show()
    sys.exit(app.exec_())

myList的每个元素都是表中的一行.

Each element of myList is a row in the table.

这篇关于了解QAbstractTableModel中的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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