starlays
13th September 2017, 07:19
Hello,
In my app I have 2 forms and I want them to share same database connection.
The first is a login window and it is the first one started when the application is lunched, after successful authentication the login window is hidden and I start the main app window.
I'm using the database connection on the main form, I'm initializing this connection in the constructor like so:
login::login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::login)
{
QString DB_FILE = QCoreApplication::applicationDirPath() + "/database.db";
ui->setupUi(this);
DB = QSqlDatabase::addDatabase("QSQLITE");
DB.setDatabaseName(DB_FILE);
QFileInfo checkFile(DB_FILE);
if(checkFile.isFile())
{
if(DB.open())
{
ui->statusbar->showMessage("[+] Connected to database");
}
}
else
{
ui->statusbar->showMessage("[!] Can't find the database file");
}
}
and the other window is showed after a successful login on a button click action like so:
void login::on_pushButton_clicked()
{
...
if(query.last())
{
ui->statusbar->showMessage("[+] Login success");
this->hide(); //hide the login window
wtimelog->show(); //show the app window
}
else
{
ui->statusbar->showMessage("[!] Wrong username or password");
}
...
}
I wander how can I make the already opened connection to the database available for the wtimelog window. At the moment it does not recognize any resources for the database from the main window as this resources are declared in another scope.
Thank you.
In my app I have 2 forms and I want them to share same database connection.
The first is a login window and it is the first one started when the application is lunched, after successful authentication the login window is hidden and I start the main app window.
I'm using the database connection on the main form, I'm initializing this connection in the constructor like so:
login::login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::login)
{
QString DB_FILE = QCoreApplication::applicationDirPath() + "/database.db";
ui->setupUi(this);
DB = QSqlDatabase::addDatabase("QSQLITE");
DB.setDatabaseName(DB_FILE);
QFileInfo checkFile(DB_FILE);
if(checkFile.isFile())
{
if(DB.open())
{
ui->statusbar->showMessage("[+] Connected to database");
}
}
else
{
ui->statusbar->showMessage("[!] Can't find the database file");
}
}
and the other window is showed after a successful login on a button click action like so:
void login::on_pushButton_clicked()
{
...
if(query.last())
{
ui->statusbar->showMessage("[+] Login success");
this->hide(); //hide the login window
wtimelog->show(); //show the app window
}
else
{
ui->statusbar->showMessage("[!] Wrong username or password");
}
...
}
I wander how can I make the already opened connection to the database available for the wtimelog window. At the moment it does not recognize any resources for the database from the main window as this resources are declared in another scope.
Thank you.