model view controller - Understanding MVC in a QAbstractTableModel -
i have data represented class of own ; fix ideas give example.
class myownmodel(): def __init__(self, name="", number=0): self.name = name self.number = number
i have list of such instances, want represent in qtableview.
li = [myownmodel("a", 1), myownmodel("b", 2)]
then see 2 strategies make qtableview :
- change myownmodel subclasses qabstracttablemodel
- build new qabstracttablemodel mimics myownmodel in way attributes instance 2 qstring , connect datachanged signal function updates instance of myownmodel
i not satisfied of these, have no other idea moment...
which 1 suitable problem ? (i have more complex class in practice use same framework)
as stated in comment, model list of object. should subclass qabstracttablemodel
use list.
here's code snippet this:
import sys import signal import pyqt4.qtcore pcore import pyqt4.qtgui 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 print in 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 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 able 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_())
each element of mylist
row in table.
Comments
Post a Comment