[PyQt] PyQt book: problem with database example

Phil Thompson phil at riverbankcomputing.co.uk
Thu Jan 10 14:46:08 GMT 2008


On Thursday 10 January 2008, Mark Summerfield wrote:
> On 2008-01-09, Sibylle Koczian wrote:
> > Hello,
> >
> > the assetmanager.pyw example doesn't seem to work correctly, or I'm using
> > it wrongly:
> >
> > - The logs view always shows one empty line after the records
> > belonging to the selected asset. Why? When I try to write such a
> > master-detail dialog myself, using the asset manager as a model, I get
> > even more empty lines, and their number changes. Should I try to
> > shrink it to a minimal example still showing the problem, or is this a
> > known issue?
> >
> > - After leaving the application I get the error message
> > "QSqlDatabasePrivate::removeDatabase: connection
> > 'qt_sql_default_connection' is still in use, all queries will cease to
> > work."  I have had this message in my own programs - it went away,
> > when I didn't define a "main" funktion, but put its content directly
> > under "if __name__ == '__main__':". But I never understood why this
> > should be so.
>
> Hi Koczian,
>
> I had experienced both these problems when developing the application,
> but neither occurred when I tested on Linux & Windows at the time the
> book was published. Since then I've upgraded to PyQt 4.3.1/Qt 4.3.2, and
> now both problems are back.
>
> The too many rows bug is a problem with QTableView. When setFilter() is
> applied, followed by select(), the correct rows are selected and Qt
> knows how many rows there are (which you can verify by putting "print
> self.logModel.rowCount()" after the select() call). But the QTableView
> shows twice as many rows as are in the model, with the extra ones shown
> empty. This only seems to happen when setFilter() is used (which may be
> why the problem doesn't affect the assets table), and the underlying SQL
> table is not affected. So this seems to be a Qt bug---one that seems to
> come and go with different minor versions unfortunately!
>
> I checked the Trolltech "Task Tracker" (bug tracker) and it looks like
> this problem is bug 170783 which was scheduled to be fixed in Qt
> 4.3.2---but I'm using Qt 4.3.2 and the bug is still present.
>
> The error message is harmless, but annoying. I don't think it is due to
> any bug in the application, but rather a problem in PyQt regarding the
> lifetime of the default connection, so I'm passing the buck to Phil on
> this one.

Yes it's to do with the (random) order that objects get garbage collected when 
the interpreter exits. You need to make sure that the objects that internally 
reference the default connection are deleted before the QApplication 
instance.

In the case of assetmanager placing the following lines after the call to 
app.exec_() does the trick...

    del form
    del db

Unfortunately I can't do anything about this as QSqlDatabase isn't a QObject, 
or even has a virtual dtor.

Phil


More information about the PyQt mailing list