Greetings,

I'm experimenting with QAbstractTableModel + QTableView and having a
difficult time getting what I expect.  I'm hoping with the attached
code to create a grid with a header and each cell showing some text.
Instead I'm getting a grid with no header (tableview.header().show()
has no effect) and cells that look like check box widgets.

Can anyone spot some obvious problems with my code?

With QTableView does one have to manually create header and enable
column resizing?  Or is there some hidden switch that I'm not touching
that enables this functionality?

-K
#!/usr/bin/env python

import sys
import os
from PyQt4 import QtGui, QtCore

class TstModel( QtCore.QAbstractTableModel ):
    
    def __init__( self ):
        QtCore.QAbstractTableModel.__init__(self)

        self._header = ( 'col0', 'col1' )
        self._itemsToKeep = 10
        self._itemIdx = 0
        self._items = [ ('item0', 'item0'),
                        ('item1','item1'),
                        ('item2','item2') ]

    def rowCount( self, parent ):
        return len(self._items)

    def columnCount( self, parent ):
        return len(self._header)

    def data( self, index, role ):
        if not index.isValid():
            return QtCore.QVariant('')

        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant('')

        row = index.row()
        col = index.column()
        
        if row < 0 or row >= len(self._items) or \
               col < 0 or col >= len(self._header):
            return QtCore.QVariant('')

        return QtCore.QVariant(self._items[row][col])

    def headerData( self, section, orientation, role ):
        if orientation == QtCore.Qt.Horizontal and \
               role == QtCore.Qt.DisplayRole:
            return QtCore.QVariant( self._header[section] )

        return QtCore.QVariant('')

    def flags( self, index ):
        return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled

class Tst:
    def run( self ):
        app = QtGui.QApplication(sys.argv)
        tm = TstModel()
        tv1 = QtGui.QTableView()
        tv1.setModel(tm)
        tv1.show()
        app.connect(app, QtCore.SIGNAL('lastWindowClosed()'), \
                    app, QtCore.SLOT('quit()'))
        sys.exit(app.exec_())

if __name__ == "__main__":
    tst = Tst()
    tst.run()

_______________________________________________
PyKDE mailing list    [email protected]
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde

Reply via email to