[PyKDE] Small database application - best way?

David Boddie david at boddie.org.uk
Mon Nov 20 20:21:15 GMT 2006

On Sun, 19 Nov 2006 15:38:41 +0100, Sibylle Koczian wrote:

> The first problem I'm trying to solve: The main window contains a
> QTableView and some controls. At the start it should be big enough to
> show all the data in the view (6-9 rows, 6 columns). I can call
> mainwindow.resize(x, y), but I'd prefer to resize the viewport, if
> possible depending on the current row count. Calling tableview.resize(x,
> y) doesn't help, that still gives a window that only shows part of the
> table. What's the right method to use?

Have you put the QTableView in a layout, or did you use QMainWindow for
the window and set the QTableView as the central widget?

> Second problem: The QTableView shouldn't be editable. Double clicking on
> a record should call a dialog window showing this record, and in this
> window some of the fields should be editable. But as far as I can see in
> the docs, edit-ability is a property of the model, not of the view.
> Anyway, I don't know how to connect a model with controls not inheriting
> one of the view classes. On the other hand, I don't think I really plan
> something so very unusual.

I don't think so, either. One way to do this is to disable edit triggers
for the view:

  tableView = QTableView()

The NoEditTriggers value is actually defined in QAbstractItemView, and
QTableView inherits it from there.

Another way is to control editing on the model itself. Here's one way to
create a model that disables editing:

  class CustomModel(QtSql.QSqlTableModel):
      def flags(self, index):
          return QtCore.Qt.ItemIsEnabled

This QSqlTableModel subclass just implements a flags() method that returns
a flag indicating that each item is enabled (it doesn't check the index it
is passed). By setting many of these, you can customize how the items appear
and whether they can be modified:


For example, you might want some them to be selectable, but not editable,
so the flags() method would look like this:

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

> And, finally: If possible I would prefer not to use the Qt database
> plugins, but to keep using the Python database module. Or is that a very
> bad idea?

Not necessarily. It has been done before. See the "Database Support"
directory in the archive available from here:


[Aside: Thanks again for this, Torsten. :-)]

> The database is Firebird 1.5.3, OS and distribution Ubuntu 6.10 (with
> Gnome as the desktop, but the necessary libraries for PyQt are
> installed), database module kinterbasdb 3.2. Everything local on the
> same machine.
> I've tried and failed to find helpful examples in the documentation, but
> of course I may simply have missed them.

Unfortunately, I can't see anything obvious in the documentation about
this, and the examples seem to need some more documentation, too. :-(
Still, means that there's the chance to improve things a bit. :-)

Hope this helped,


More information about the PyQt mailing list