Results 1 to 2 of 2

Thread: Having a QSqlDatabase as a class member and avoid the connection still in use warning

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Having a QSqlDatabase as a class member and avoid the connection still in use warning

    Hi,

    I have a class that has a QSqlDatabase as a private member.

    Qt Code:
    1. private:
    2. QSqlDatabase m_database;
    To copy to clipboard, switch view to plain text mode 

    I use m_database in several functions of the class like:

    Qt Code:
    1. QSqlQuery *query = new QSqlQuery(m_database);
    2.  
    3. ..
    4.  
    5. delete query;
    To copy to clipboard, switch view to plain text mode 

    However in the destructor of the class I remove the connection with
    Qt Code:
    1. m_database.close();
    2. QSqlDatabase::removeDatabase("MyConnection");
    To copy to clipboard, switch view to plain text mode 

    But I always get the warning "MyConnection' is still in use, all queries will cease to work."

    How can I avoid the warning?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Having a QSqlDatabase as a class member and avoid the connection still in use war

    Set the database object to an empty one before removing the database:

    Qt Code:
    1. m_database.close();
    2. const QString cname = m_database.connectionName();
    3. m_database = QSqlDatabase();
    4. QSqlDatabase::removeDatabase(cname);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    qlands (16th October 2014)

Similar Threads

  1. Avoid QMetaObject::connectSlotsByName warning
    By qt_developer in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2014, 14:30
  2. How to set x509 on a QSqlDatabase Connection?
    By m3rlin in forum Qt Programming
    Replies: 24
    Last Post: 21st February 2012, 05:04
  3. Windows OCI QSqlDatabase connection
    By hollyberry in forum Newbie
    Replies: 10
    Last Post: 13th February 2012, 23:13
  4. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  5. How to avoid to repeat a connection
    By Auryn in forum Qt Programming
    Replies: 1
    Last Post: 8th July 2009, 16:47

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.