Using Sub Classes (I think that is what im trying to do)
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:
Code:
createDatabaseConn();
and the function is like so
Code:
bool createDatabaseConn()
{
db.setHostName("myhostname");
db.setDatabaseName("mydatabasename");
db.setUserName("myusername");
db.setPassword("mypassword");
if (!db.open()) {
//QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
return false;
}
return true;
}
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 :
Code:
#ifndef MYDATABASE_H
#define MYDATABASE_H
#include <QObject>
#include <QSettings>
#include <QSqlDatabase>
{
Q_OBJECT
public:
explicit myDatabase
(QObject *parent
= 0);
signals:
public slots:
bool createDatabaseConn();
};
#endif // MYDATABASE_H
in my database.cpp
Code:
#include "mydatabase.h"
myDatabase
::myDatabase(QObject *parent
) :{
}
bool createDatabaseConn()
{
settings.beginGroup("database");
db.setHostName(settings.value("server").toString());
db.setDatabaseName("atsadmin");
db.setUserName(settings.value("databaseUsername").toString());
db.setPassword(settings.value("databasePassword").toString());
settings.endGroup();
if (!db.open()) {
//QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
return false;
}
return true;
}
So now i thought using the connection would be as simple as
Code:
#include "mydatabase.h"
rm_customer_search
::frm_customer_search(QWidget *parent
) : ui(new Ui::frm_customer_search)
{
ui->setupUi(this);
myDatabase::createDatabaseConn();
}
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
Re: Using Sub Classes (I think that is what im trying to do)
Quote:
Originally Posted by
ShapeShiftme
frm_customer_search.cpp:10: error: cannot call member function ‘bool myDatabase::createDatabaseConn()’ without object
This error message does explain the problem very clearly.
You need to create an object first.
Example:
Code:
myDatabase myDb;
myDb.createDatabaseConn();
// or
myDatabase *myDb = new myDatabase();
myDb->createDatabaseConn();
Re: Using Sub Classes (I think that is what im trying to do)
Create -in your MainWindow- only one connection with the database and pass it to every dialog that has to use it.
Something like this:
Code:
db->addDatabase("QMYSQL");
db->...etc...
...
myDialog = new MyDialog(this, db);
myDialog->exec();
...
Re: Using Sub Classes (I think that is what im trying to do)
Quote:
Originally Posted by
tbscope
This error message does explain the problem very clearly.
You need to create an object first.
Example:
Code:
myDatabase myDb;
myDb.createDatabaseConn();
// or
myDatabase *myDb = new myDatabase();
myDb->createDatabaseConn();
Thnaks very much. Very obvious when someone tells you what you are doing wrong.
And works great