Good day.

Im really new to Qt development but im now getting around nicely. However there are somethings i still find confusing.

Here is an example:
Im writing a database application or rather an application that uses mysql to access data. it works great, i can read write and manipulate data. However now that my program is starting to get bigger, in other words more than 1 Dialog / Widget. Im running to 2 what should be a simple problem.

To create my database connection I use the following call:

Qt Code:
  1. createDatabaseConn();
To copy to clipboard, switch view to plain text mode 

and the function is like so

Qt Code:
  1. bool createDatabaseConn()
  2. {
  3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  4. db.setHostName("myhostname");
  5. db.setDatabaseName("mydatabasename");
  6. db.setUserName("myusername");
  7. db.setPassword("mypassword");
  8. if (!db.open()) {
  9. //QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
  10. return false;
  11. }
  12. return true;
  13. }
To copy to clipboard, switch view to plain text mode 

But know on my second dialog i create the same function. I get an error with something like name already exists so i call the function

createDatabaseConn2 and on the next dialog createDatabaseConn3.

However this is a complete waste in vb.net i would just create a module with the function the from all my forms i would be able to use the function.
So now im trying to do this and this is where im comming into trouble.

i created a c++ class mydatabase.h

with the following :

Qt Code:
  1. #ifndef MYDATABASE_H
  2. #define MYDATABASE_H
  3.  
  4. #include <QObject>
  5. #include <QSettings>
  6. #include <QSqlDatabase>
  7.  
  8. class myDatabase : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit myDatabase(QObject *parent = 0);
  13.  
  14. signals:
  15.  
  16. public slots:
  17. bool createDatabaseConn();
  18.  
  19. };
  20.  
  21. #endif // MYDATABASE_H
To copy to clipboard, switch view to plain text mode 

in my database.cpp
Qt Code:
  1. #include "mydatabase.h"
  2.  
  3.  
  4. myDatabase::myDatabase(QObject *parent) :
  5. QObject(parent)
  6. {
  7. }
  8.  
  9.  
  10. bool createDatabaseConn()
  11. {
  12. QSettings settings("ATSTech", "mysettings");
  13.  
  14. settings.beginGroup("database");
  15.  
  16. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  17.  
  18. db.setHostName(settings.value("server").toString());
  19. db.setDatabaseName("atsadmin");
  20. db.setUserName(settings.value("databaseUsername").toString());
  21. db.setPassword(settings.value("databasePassword").toString());
  22.  
  23. settings.endGroup();
  24.  
  25. if (!db.open()) {
  26. //QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
  27. return false;
  28. }
  29. return true;
  30. }
To copy to clipboard, switch view to plain text mode 

So now i thought using the connection would be as simple as


Qt Code:
  1. #include "mydatabase.h"
  2.  
  3. rm_customer_search::frm_customer_search(QWidget *parent) :
  4. QDialog(parent),
  5. ui(new Ui::frm_customer_search)
  6. {
  7. ui->setupUi(this);
  8.  
  9. myDatabase::createDatabaseConn();
  10. }
To copy to clipboard, switch view to plain text mode 

And what do you know what a noob i must be i was WRONG.
i get the following error
frm_customer_search.cpp:10: error: cannot call member function ‘bool myDatabase::createDatabaseConn()’ without object

Could someone please let me know what im doing wrong or if im on the compltly wrong track could you let me know what i should be doing .

Regards