[Qt-interest] Open database in constructor?
Bill KING
bill.king at trolltech.com
Fri Jan 2 05:16:45 CET 2009
The problem is the model needs the database at constructor time. I'm
assuming if there's no db supplied, then it defaults to the system
default database (which is hte first database opened).
So... when the database is opened before the constructor, it's set as
the system default database, which is used (correctly) by the data
model. When it's opened inside the constructor, there's no system
default database, and hence fails.
To work around, I'd create the datamodel dynamically via new, after
creating the database, and passing db to the constructor.
Israel Brewster wrote:
> Is it not possible to open a database connection in the constructor of
> a class (QMainWindow based, in this case) and have it work? I tried
> adding the following lines of code to the constructor of my class:
>
> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
> db.setDatabaseName("xxxxx");
> if(!db.open())
> {
> QMessageBox::critical(this, tr("Datastore"),
> "Unable to open database!");
> return;
> }
> dataModel.setTable("yyy");
> if (dataModel.lastError().isValid())
> {
> ...
>
> In this case, I always get a "could not find table yyy" error.
> However, if I take the database opening code and put it before the
> constructor (i.e. just before instantiating my object) without
> changing any of the code, it works perfectly- albeit with the
> requirement of a lot of code duplication, as there are several places
> I could be opening this from. Thus the reason I just want to put it in
> the constructor. Am I doing something wrong here? Or is this just not
> possible? Thanks.
> -----------------------------------------------
> Israel Brewster
> Computer Support Technician
> Frontier Flying Service Inc.
> 5245 Airport Industrial Rd
> Fairbanks, AK 99709
> (907) 450-7250 x293
> -----------------------------------------------
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
--
Bill King, Software Engineer
Qt Software, Nokia Pty Ltd
Brisbane Office
More information about the Qt-interest-old
mailing list