[Qt-interest] Open database in constructor?
Israel Brewster
israel at frontierflying.com
Fri Jan 2 21:44:37 CET 2009
Yep, that did the trick. And makes perfect sense, now that I think
about it. Thanks much!
-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
On Jan 1, 2009, at 7:16 PM, Bill KING wrote:
> 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