[Qt-interest] Open database in constructor?
Israel Brewster
israel at frontierflying.com
Thu Jan 1 00:14:05 CET 2009
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
-----------------------------------------------
More information about the Qt-interest-old
mailing list