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:
ui(new Ui::login)
{
ui->setupUi(this);
DB.setDatabaseName(DB_FILE);
if(checkFile.isFile())
{
if(DB.open())
{
ui->statusbar->showMessage("[+] Connected to database");
}
}
else
{
ui->statusbar->showMessage("[!] Can't find the database file");
}
}
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");
}
}
To copy to clipboard, switch view to plain text mode
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");
}
...
}
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");
}
...
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks