理解 QAbstractTableModel 中的 MVC
Understanding MVC in a QAbstractTableModel
我有一些数据,用我自己的 class 表示;修正思路我举个例子。
class MyOwnModel():
def __init__(self, name="", number=0):
self.name = name
self.number = number
然后我有一个这样的实例列表,我想在 QTableView
中表示。
li = [MyOwnModel("a", 1), MyOwnModel("b", 2)]
然后我看到两个策略可以从中得到 QTableView
:
- 更改
MyOwnModel
使其低于 classes QAbstractTableModel
- 构建一个新的
QAbstractTableModel
模仿 MyOwnModel
的方式,例如它的属性是两个 QString
并将 dataChanged
信号连接到更新MyOwnModel
的实例
这些我都不是很满意,暂时也没有别的想法。
哪一个最适合我的问题? (我在实践中有一个更复杂的 class 但我想使用相同的框架)
如评论中所述,您的模型就是您的对象列表。您应该继承 QAbstractTableModel
以使用此列表。
这是我的代码片段:
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
的每个元素都是 table 中的一行。
我有一些数据,用我自己的 class 表示;修正思路我举个例子。
class MyOwnModel():
def __init__(self, name="", number=0):
self.name = name
self.number = number
然后我有一个这样的实例列表,我想在 QTableView
中表示。
li = [MyOwnModel("a", 1), MyOwnModel("b", 2)]
然后我看到两个策略可以从中得到 QTableView
:
- 更改
MyOwnModel
使其低于 classesQAbstractTableModel
- 构建一个新的
QAbstractTableModel
模仿MyOwnModel
的方式,例如它的属性是两个QString
并将dataChanged
信号连接到更新MyOwnModel
的实例
这些我都不是很满意,暂时也没有别的想法。
哪一个最适合我的问题? (我在实践中有一个更复杂的 class 但我想使用相同的框架)
如评论中所述,您的模型就是您的对象列表。您应该继承 QAbstractTableModel
以使用此列表。
这是我的代码片段:
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
的每个元素都是 table 中的一行。