[PyQt] QAbstractTableModel problems in PyQt4

Mark Summerfield mark at qtrac.eu
Mon Nov 5 10:13:31 GMT 2007


On 2007-11-05, Vladimir Pouzanov wrote:
> Hi all,
>
> I'm trying to implement a table model:
> class AModel(QtCore.QAbstractTableModel):
>     def __init__(self, parent=None):
>         QtCore.QAbstractTableModel.__init__(self, parent)
>
>     def rowCount(self, parent=QtCore.QModelIndex()):
>         print "row count rq"
>         return 3
>
>     def columnCount(self, parent=QtCore.QModelIndex()):
>         print "col count rq"
>         return 3
>
>     def data(self, idx, role=QT.DisplayRole):
>         print "data rq"
>         return QtCore.QVariant("test")
>
> ...
>
> self.ui.tableView.setModel(AModel())
>
> the problem is that data is never called (rowCount/columnCount are called
> three times each), so nothing is rendered. What do I mess?

There might be a problem with this:

    self.ui.tableView.setModel(AModel())

I think that the AModel instance needs to be kept alive, either by
giving it a parent or by making it an instance variable, for example
(untested):

    self.model = AModel()
    self.ui.tableView.setModel(self.model)

    # OR
    
    self.ui.tableView.setModel(AModel(self))

-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu



More information about the PyQt mailing list