Hi.

I'm trying to implement a generic connection class using Qt for my projects.

The code looks like this:

connection.h
Qt Code:
  1. #ifndef CONNECTION_H
  2. #define CONNECTION_H
  3.  
  4. #include <QtSql/QSqlDatabase>
  5.  
  6. class Connection
  7. {
  8. public:
  9. bool open();
  10. void close();
  11. bool isOpen() const;
  12.  
  13. QString databaseName() const;
  14. QString hostName() const;
  15. QString userName() const;
  16. QString password() const;
  17. int port() const;
  18.  
  19. QString lastError() const;
  20.  
  21. operator QSqlDatabase();
  22. operator QSqlDatabase*();
  23.  
  24. protected:
  25. void setDatabaseName(const QString &databaseName);
  26. void setHostName(const QString &hostName);
  27. void setUserName(const QString &userName);
  28. void setPassword(const QString &password);
  29. void setPort(int port);
  30.  
  31. protected:
  32. };
  33.  
  34. #endif // CONNECTION_H
To copy to clipboard, switch view to plain text mode 

connection.cpp
Qt Code:
  1. #include <QtSql/QSqlError>
  2.  
  3. #include "connection.h"
  4.  
  5. bool Connection::open()
  6. {
  7. return m_db.open();
  8. }
  9.  
  10. void Connection::close()
  11. {
  12. if (isOpen())
  13. m_db.close();
  14. }
  15.  
  16. bool Connection::isOpen() const
  17. {
  18. return m_db.isOpen();
  19. }
  20.  
  21. QString Connection::databaseName() const
  22. {
  23. return m_db.databaseName();
  24. }
  25.  
  26. QString Connection::hostName() const
  27. {
  28. return m_db.hostName();
  29. }
  30.  
  31. QString Connection::userName() const
  32. {
  33. return m_db.userName();
  34. }
  35.  
  36. QString Connection::password() const
  37. {
  38. return m_db.password();
  39. }
  40.  
  41. int Connection::port() const
  42. {
  43. return m_db.port();
  44. }
  45.  
  46. QString Connection::lastError() const
  47. {
  48. return m_db.lastError().databaseText();
  49. }
  50.  
  51. Connection::operator QSqlDatabase()
  52. {
  53. return m_db;
  54. }
  55.  
  56. Connection::operator QSqlDatabase *()
  57. {
  58. return &m_db;
  59. }
  60.  
  61. void Connection::setDatabaseName(const QString &databaseName)
  62. {
  63. if (isOpen())
  64. close();
  65.  
  66. if (m_db.databaseName() != databaseName)
  67. m_db.setDatabaseName(databaseName);
  68. }
  69.  
  70. void Connection::setHostName(const QString &hostName)
  71. {
  72. if (isOpen())
  73. close();
  74.  
  75. m_db.setHostName(hostName);
  76. }
  77.  
  78. void Connection::setUserName(const QString &userName)
  79. {
  80. if (isOpen())
  81. close();
  82.  
  83. m_db.setUserName(userName);
  84. }
  85.  
  86. void Connection::setPassword(const QString &password)
  87. {
  88. if (isOpen())
  89. close();
  90.  
  91. m_db.setPassword(password);
  92. }
  93.  
  94. void Connection::setPort(int port)
  95. {
  96. if (isOpen())
  97. close();
  98.  
  99. m_db.setPort(port);
  100. }
To copy to clipboard, switch view to plain text mode 

sqliteconnection.h
Qt Code:
  1. #ifndef SQLITECONNECTION_H
  2. #define SQLITECONNECTION_H
  3.  
  4. class Connection;
  5.  
  6. class SQLiteConnection : public Connection
  7. {
  8. public:
  9. SQLiteConnection();
  10. SQLiteConnection(const QString &databaseName);
  11.  
  12. bool open(const QString &databaseName);
  13. };
  14.  
  15. #endif // SQLITECONNECTION_H
To copy to clipboard, switch view to plain text mode 

sqliteconnection.cpp
Qt Code:
  1. #include <QtCore/QUuid>
  2.  
  3. #include "connection.h"
  4. #include "sqliteconnection.h"
  5.  
  6. SQLiteConnection::SQLiteConnection()
  7. {
  8. m_db = QSqlDatabase::addDatabase("QSQLITE", QUuid::createUuid().toString());
  9. }
  10.  
  11. SQLiteConnection::SQLiteConnection(const QString &databaseName)
  12. {
  13. SQLiteConnection();
  14.  
  15. Connection::setDatabaseName(databaseName);
  16. }
  17.  
  18. bool SQLiteConnection::open(const QString &databaseName)
  19. {
  20. Connection::setDatabaseName(databaseName);
  21.  
  22. return Connection::open();
  23. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <QtWidgets/QApplication>
  2.  
  3. #include "monetae.h"
  4.  
  5. #include "db/connection.h"
  6. #include "db/sqliteconnection.h"
  7. #include "db/mysqlconnection.h"
  8.  
  9. #include <QtSql/QSqlQuery>
  10. #include <QtCore/QDebug>
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. QApplication application(argc, argv);
  15.  
  16. Connection con = SQLiteConnection("~/test.sq3");
  17.  
  18. if (con.open() == false) {
  19. qDebug() << con.lastError();
  20.  
  21. return -1;
  22. }
  23.  
  24. QSqlQuery qry(con);
  25.  
  26. qry.exec("CREATE TABLE TEST (ID INTEGER, NAME VARCHAR(10))");
  27.  
  28. return 0;
To copy to clipboard, switch view to plain text mode 

The problem is that con.open() fails and con.lastError() returns "Driver not loaded". But if m_db is a private member in SqliteConnection and not a protected member in Connection everything works fine.