[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyKDE] Abstract Table Model + TableView
From: "Kelly Burkhart" <kelly.burkhart () gmail ! com>
Date: 2007-01-31 1:10:53
Message-ID: fa1e4ce70701301710p5a3c4dffy55f029f329c4e237 () mail ! gmail ! com
[Download RAW message or body]
That works perfectly. Thanks!
-K
On 1/30/07, David Boddie <david@boddie.org.uk> wrote:
> On Tue Jan 30 23:45:23 MET 2007, Kelly Burkhart wrote:
>
> > 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?
>
> Yes, you're returning QVariants containing empty strings when you mean
> to return invalid QVariants. Change all occurrences of QVariant('') to
> QVariant() and it should work as you expect.
>
> > 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?
>
> It should just work. There are some subtleties with expanding columns
> and rows to optimize the space allocated to items - just ask again if
> you encounter problems along those lines.
>
> David
>
> _______________________________________________
> PyKDE mailing list PyKDE@mats.imk.fraunhofer.de
> http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
>
["tst_tv.py" (text/x-python)]
#!/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', 'col2' )
self._itemsToKeep = 10
self._itemIdx = 0
self._items = [ ('item0', 'item0', 'item0'),
('item1','item1','item1'),
('item2','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 TstTableView( QtGui.QTableView ):
def __init__( self, model ):
QtGui.QTableView.__init__(self)
self.setModel(model)
self.verticalHeader().hide()
self.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Interactive)
self.horizontalHeader().setResizeMode(1, QtGui.QHeaderView.Interactive)
self.horizontalHeader().setResizeMode(2, QtGui.QHeaderView.Stretch)
class Tst:
def run( self ):
app = QtGui.QApplication(sys.argv)
tm = TstModel()
tv1 = TstTableView( 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 PyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic