ShapeShiftme
15th January 2011, 13:15
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:
createDatabaseConn();
and the function is like so
bool createDatabaseConn()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
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 :
#ifndef MYDATABASE_H
#define MYDATABASE_H
#include <QObject>
#include <QSettings>
#include <QSqlDatabase>
class myDatabase : public QObject
{
Q_OBJECT
public:
explicit myDatabase(QObject *parent = 0);
signals:
public slots:
bool createDatabaseConn();
};
#endif // MYDATABASE_H
in my database.cpp
#include "mydatabase.h"
myDatabase::myDatabase(QObject *parent) :
QObject(parent)
{
}
bool createDatabaseConn()
{
QSettings settings("ATSTech", "mysettings");
settings.beginGroup("database");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
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
#include "mydatabase.h"
rm_customer_search::frm_customer_search(QWidget *parent) :
QDialog(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
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:
createDatabaseConn();
and the function is like so
bool createDatabaseConn()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
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 :
#ifndef MYDATABASE_H
#define MYDATABASE_H
#include <QObject>
#include <QSettings>
#include <QSqlDatabase>
class myDatabase : public QObject
{
Q_OBJECT
public:
explicit myDatabase(QObject *parent = 0);
signals:
public slots:
bool createDatabaseConn();
};
#endif // MYDATABASE_H
in my database.cpp
#include "mydatabase.h"
myDatabase::myDatabase(QObject *parent) :
QObject(parent)
{
}
bool createDatabaseConn()
{
QSettings settings("ATSTech", "mysettings");
settings.beginGroup("database");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
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
#include "mydatabase.h"
rm_customer_search::frm_customer_search(QWidget *parent) :
QDialog(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