Results 1 to 9 of 9

Thread: Qt databse connection not available in multi window app

  1. #1
    Join Date
    Sep 2016
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt databse connection not available in multi window app

    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.

  2. #2
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt databse connection not available in multi window app

    I think you are doing it wrong. Why do you open Mainwindow from Login form? Open Login form from Mainwindow and then e.g get DB from Login or you can define DB in Mainwindow and put pointer into Login form.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt databse connection not available in multi window app

    Why do you open Mainwindow from Login form?
    I can sort of see the logic in this. If the login is not successful, you don't want the user to be able to access any of the functionality of the main window. But I agree that it is better to keep the DB as a member of the MainWindow class (or another non-GUI class that manages DB access). You can pass a pointer to the DB instance (or to a QSqlQuery instance) to the Login window.

    You can guarantee that the Login dialog is displayed immediately on startup by adding a zero time single-shot QTimer in the MainWindow constructor. Connect the timer's timeout() signal to a MainWindow slot that calls exec() on the login dialog. The single-shot will fire as soon as the MainWindow's showEvent() completes and display the login. If the login fails, you can then call close() on your MainWindow to shut down the app (or give the user another try to login).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt databse connection not available in multi window app

    Quote Originally Posted by d_stranz View Post
    I can sort of see the logic in this. If the login is not successful, you don't want the user to be able to access any of the functionality of the main window.
    As you wrote, he can make a signal-slot with bool value and check if the login was successful. Or in Login define bool value and after mainWindow calls exec() wait for true in while loop, so the program will start only after successfully logged or close the app.

  5. #5
    Join Date
    Sep 2016
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt databse connection not available in multi window app

    Thank you for the help, I will change the app.

    Now it remains to find out how can I pass a pointer to the other window. Should it be done as an argument to the constructor?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt databse connection not available in multi window app

    Now it remains to find out how can I pass a pointer to the other window. Should it be done as an argument to the constructor?
    That's one way. You could also simply pass it with a setter method: void Login:: setDBPointer( QSqlDatabase * pDB ) that you call after you create the Login instance but before you call exec(). You'll need to write this method, of course.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt databse connection not available in multi window app

    You do not need to pass a QSqlDatabase pointer. Read about QSqlDatabase::database.

  8. The following 2 users say thank you to Lesiok for this useful post:

    d_stranz (15th September 2017), starlays (15th September 2017)

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt databse connection not available in multi window app

    Yes, Lesiok is absolutely correct- I had forgotten about this. So, add and open the database in MainWIndow, then simply call QSqlDatabase:: database() from inside Login to get the reference to that database. No need to pass anything to Login.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #9
    Join Date
    Sep 2016
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt databse connection not available in multi window app

    Thank you guys, problem solved, changed the way windows are displayed and passed the database connection as a parameter and now it is working.

Similar Threads

  1. Multi-Window Application
    By bernard in forum Qt Programming
    Replies: 6
    Last Post: 30th October 2012, 04:32
  2. Multi Window Application in QT
    By GopakumarG in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2011, 07:42
  3. Replies: 0
    Last Post: 18th November 2010, 17:48
  4. Multi-window document structure
    By JariV in forum Newbie
    Replies: 2
    Last Post: 16th February 2009, 21:42
  5. Multi Window Application in QT/C++
    By pshah.mumbai in forum Newbie
    Replies: 8
    Last Post: 8th July 2008, 18:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.