Results 1 to 2 of 2

Thread: QSqlDatabase strange behavior

  1. #1
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation QSqlDatabase strange behavior

    Hi everyone,
    I am trying mysql database connection with QSqlDatabase and it's behaving really strange. When I have only one button in the GUI, everything works fine. But when I add Line Edit, which doesn't do anything, program hangs for about 4 seconds on exit.
    Only thing the program does is that it creates connection to database in constructor and then closes it in destructor.

    Here is the code:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. db = QSqlDatabase::addDatabase("QMYSQL", "connection");
    7. db.setHostName("127.0.0.1");
    8. db.setDatabaseName("db");
    9. db.setUserName("user");
    10. db.setPassword("password");
    11. connected = db.open();
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. db.close();
    17. db = QSqlDatabase();
    18. QSqlDatabase::removeDatabase("connection");
    19. delete ui;
    20. }
    To copy to clipboard, switch view to plain text mode 

    If I put this code in "on_pushButton_clicked()" function, program exits correctly. But is it okay to create and remove connection to database in every function of the program, when I want to create more complex database solution?

    Another strange thing is that I had to add "db = QSqlDatabase();" before I call removeDatabase, because it showed warning about connection being used when program exited.

    Is someone here who can explain it to me, please?

  2. #2
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase strange behavior

    I forgot to mention that this issue is on windows, it is working properly on linux. So maybe some bug?

Similar Threads

  1. QAudioInput example strange behavior
    By m15ch4 in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2010, 06:55
  2. Qt Creator Strange behavior of GDB in Creator 1.3
    By rayjc in forum Qt Tools
    Replies: 1
    Last Post: 11th March 2010, 21:42
  3. QComboBox strange behavior
    By Antebios in forum Newbie
    Replies: 2
    Last Post: 31st March 2009, 23:50
  4. Strange resize behavior
    By Lykurg in forum Newbie
    Replies: 3
    Last Post: 9th January 2007, 13:56
  5. scrollbars strange behavior
    By siniy in forum Qt Programming
    Replies: 6
    Last Post: 29th December 2006, 10:27

Tags for this Thread

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.