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:

Qt Code:
  1. login::login(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::login)
  4. {
  5. QString DB_FILE = QCoreApplication::applicationDirPath() + "/database.db";
  6.  
  7. ui->setupUi(this);
  8.  
  9. DB = QSqlDatabase::addDatabase("QSQLITE");
  10. DB.setDatabaseName(DB_FILE);
  11. QFileInfo checkFile(DB_FILE);
  12.  
  13. if(checkFile.isFile())
  14. {
  15. if(DB.open())
  16. {
  17. ui->statusbar->showMessage("[+] Connected to database");
  18. }
  19. }
  20. else
  21. {
  22. ui->statusbar->showMessage("[!] Can't find the database file");
  23. }
  24. }
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:

Qt Code:
  1. void login::on_pushButton_clicked()
  2. {
  3. ...
  4. if(query.last())
  5. {
  6. ui->statusbar->showMessage("[+] Login success");
  7. this->hide(); //hide the login window
  8. wtimelog->show(); //show the app window
  9. }
  10. else
  11. {
  12. ui->statusbar->showMessage("[!] Wrong username or password");
  13. }
  14. ...
  15. }
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.