[PyKDE] Making a column read-only in QTableWidget

Andreas Pakulat apaku at gmx.de
Fri Sep 8 16:33:53 BST 2006


On 08.09.06 07:53:36, Chris Dunscombe wrote:
> 
> 
> --- Andreas Pakulat <apaku at gmx.de> wrote:
> 
> > On 08.09.06 04:29:21, Chris Dunscombe wrote:
> > > I'm porting an app from PyQt3 to PyQt4 but can't find any obvious replacement for
> > > QTable.setColumnReadonly() in QTableWidget.
> > 
> > Use QTableWidgetItem::setFlags and don't include the Qt.IsEditable flag.
> > 
> 
> Thanks for that, maybe I'm being thick but does this mean I need to make each cell a
> QTableWidgetItem?

All cells of a QTableWidget that contain data are QTableWidgetItems.

> If possible could you provide a simple short example to complete the following:
> 
> myTable = QTableWidget(self)
> myTable.setColumnCount(2)

That looks pythonic ;)

for row in myTable.rowCount():
  if myTable.item(row, 0) is not None:
    myTable.item(row, 0).setFlags(Qt.IsSelectable)

Of course this only works if the table is already populated, if you
populate it later on with something like:

myTable.setItem(somerow,somecol, QTableWidgetItem(somestring))

do it in 2 steps instead for the column 0:

item = QTableWidgetItem(somestring)
item.setFlags(Qt.IsSelectable)
myTable.setItem(somerow,somecol,item)

> > > Any ideas? (BTW I can't use the SQL module for licensing reasons)
> > 
> > Why's that? I might be wrong, but afaik the SQL Module from Qt and PyQt
> > are under the same license as the rest of it. The headers in the sql
> > module say so too (i.e. GPL). Or is this different on windows?
> > 
> 
> No difference on Windows but I'm using the commercial version which under Qt has a number of
> different Editions each with different modules.

Ah, had no experience with the commercial version yet.

> > And anyways: What does the sql module have to do with a table widget?
> > 
> The SQL module does have a "table view" which might be useful in this case.

No it doesn't. It has a QTableModel, which can be hooked into a
QTableView. But you'd need a SQL database for this and they'd be
editable too, I think.

If you have a database underlying, maybe you should consider
implementing your own model on top of it and using a plain QTableView to
display the data. That way you don't have to set each individual item to
be non-editable, you could more easily do it for a complete row.

Check the examples that come with PyQt4, there are a couple of custom
models in there. Also with QAbstractTableModel as base you don't have
much more to do than implement flags, data, setData, row und
columnCount.

Andreas

-- 
You will inherit millions of dollars.




More information about the PyQt mailing list