I have attached the full code.
I have now modified it and found something strange. I pass the db object to the dialogue and then copy it to another object so it can be visibe to the rest of the class. By manipulating the copy of the objcect is like manipulating the original object which cant be true in c++.
in my example i use pointers but the result would have been exactly the same if i have passed the object to the sindesi object and then coppied to another object which is visible to the entire class.
I found in the documentation :
To make programming more convenient, QSqlDatabase is a value class. Any changes done to a database connection through one QSqlDatabase object will affect other QSqlDatabase objects representing the same connection.
I dont know if that expllains the fact that when i am coping the QSqlDatabase object and using this copy is like i am using the original.
to demonstarte that i will show the two versions of the code which shouldnt be equivalnet but they are:
the pointers version:
kentriko class implementation:
#include "kentriko.h"
#include <QMdiSubWindow>
kentriko
::kentriko(QWidget *parent
){
ui.setupUi(this);
connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
}
void kentriko::anixe_sindesi(){
sindesi *sindesi1 = new sindesi(0,&db);
sindesi1->show();
}
and the header file:
#ifndef KENTRIKO_H
#define KENTRIKO_H
#include <QtGui/QMainWindow>
#include "ui_kentriko.h"
#include <QtSql>
#include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
#include "../parathrira/analisi/analisi.h"
{
Q_OBJECT
public:
~kentriko();
public slots:
void anixe_analisi ();
void anixe_sindesi ();
private:
Ui::kentrikoClass ui;
};
kentriko class implementation:
#include "kentriko.h"
#include <QMdiSubWindow>
kentriko::kentriko(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
db = QSqlDatabase::addDatabase("QMYSQL");
}
void kentriko::anixe_sindesi(){
sindesi *sindesi1 = new sindesi(0,&db);
sindesi1->show();
}
and the header file:
#ifndef KENTRIKO_H
#define KENTRIKO_H
#include <QtGui/QMainWindow>
#include "ui_kentriko.h"
#include <QtSql>
#include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
#include "../parathrira/analisi/analisi.h"
class kentriko : public QMainWindow
{
Q_OBJECT
public:
kentriko(QWidget *parent = 0);
~kentriko();
QSqlDatabase db;
public slots:
void anixe_analisi ();
void anixe_sindesi ();
private:
Ui::kentrikoClass ui;
};
To copy to clipboard, switch view to plain text mode
and the dialogue class:
#include "sindesi.h"
#include <QtSql>
{
db5= db;// in thsi case i pass the pointer
ui.setupUi(this);
connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
}
void sindesi::sindesou() {
if (!db5->isOpen()) {
db5->setDatabaseName(ui.pinakas->text());
db5->setUserName(ui.onoma->text());
db5->setPassword(ui.kodikos->text());
db5->setHostName("127.0.0.1");
db5->setPort(3306);
if (!db5->open()) {
ui.katastasi->setText("not connected");
} else {
ui.katastasi->setText("connected");
}
} else {
ui.katastasi->setText("it has already been connected");
}
}
and the header:
#ifndef SINDESI_H
#define SINDESI_H
#include <QtSql>
#include <QtGui/QDialog>
#include "ui_sindesi.h"
{
Q_OBJECT
public:
~sindesi();
public slots:
void sindesou();
void aposindesou();
private:
Ui::sindesiClass ui;
};
#include "sindesi.h"
#include <QtSql>
sindesi::sindesi(QWidget *parent,QSqlDatabase* db)
: QDialog(parent)
{
db5= db;// in thsi case i pass the pointer
ui.setupUi(this);
connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
}
void sindesi::sindesou() {
if (!db5->isOpen()) {
db5->setDatabaseName(ui.pinakas->text());
db5->setUserName(ui.onoma->text());
db5->setPassword(ui.kodikos->text());
db5->setHostName("127.0.0.1");
db5->setPort(3306);
if (!db5->open()) {
ui.katastasi->setText("not connected");
} else {
ui.katastasi->setText("connected");
}
} else {
ui.katastasi->setText("it has already been connected");
}
}
and the header:
#ifndef SINDESI_H
#define SINDESI_H
#include <QtSql>
#include <QtGui/QDialog>
#include "ui_sindesi.h"
class sindesi : public QDialog
{
Q_OBJECT
public:
sindesi(QWidget *parent,QSqlDatabase *db);
~sindesi();
QSqlDatabase *db5;
public slots:
void sindesou();
void aposindesou();
private:
Ui::sindesiClass ui;
};
To copy to clipboard, switch view to plain text mode
and below the version where the whole object is passed:
kentriko class implementation:
#include "kentriko.h"
#include <QMdiSubWindow>
kentriko
::kentriko(QWidget *parent
){
ui.setupUi(this);
connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
}
void kentriko::anixe_sindesi(){
sindesi *sindesi1 = new sindesi(0,&db);
sindesi1->show();
}
and the header file:
#ifndef KENTRIKO_H
#define KENTRIKO_H
#include <QtGui/QMainWindow>
#include "ui_kentriko.h"
#include <QtSql>
#include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
#include "../parathrira/analisi/analisi.h"
{
Q_OBJECT
public:
~kentriko();
public slots:
void anixe_analisi ();
void anixe_sindesi ();
private:
Ui::kentrikoClass ui;
};
#include "kentriko.h"
#include <QMdiSubWindow>
kentriko::kentriko(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
db = QSqlDatabase::addDatabase("QMYSQL");
}
void kentriko::anixe_sindesi(){
sindesi *sindesi1 = new sindesi(0,&db);
sindesi1->show();
}
and the header file:
#ifndef KENTRIKO_H
#define KENTRIKO_H
#include <QtGui/QMainWindow>
#include "ui_kentriko.h"
#include <QtSql>
#include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
#include "../parathrira/analisi/analisi.h"
class kentriko : public QMainWindow
{
Q_OBJECT
public:
kentriko(QWidget *parent = 0);
~kentriko();
QSqlDatabase db;
public slots:
void anixe_analisi ();
void anixe_sindesi ();
private:
Ui::kentrikoClass ui;
};
To copy to clipboard, switch view to plain text mode
and the dialogue class:
#include "sindesi.h"
#include <QtSql>
{
db5= db;// here i pass and copy the whole object and via the db5 the db is still accesible
ui.setupUi(this);
connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
}
void sindesi::sindesou() {
if (!db5.isOpen()) {
db5.setDatabaseName(ui.pinakas->text());
db5.setUserName(ui.onoma->text());
db5.setPassword(ui.kodikos->text());
db5.setHostName("127.0.0.1");
db5.setPort(3306);
if (!db5.open()) {
ui.katastasi->setText("not connected");
} else {
ui.katastasi->setText("connected");
}
} else {
ui.katastasi->setText("it has already been connected");
}
}
and the header:
#ifndef SINDESI_H
#define SINDESI_H
#include <QtSql>
#include <QtGui/QDialog>
#include "ui_sindesi.h"
{
Q_OBJECT
public:
~sindesi();
public slots:
void sindesou();
void aposindesou();
private:
Ui::sindesiClass ui;
};
#include "sindesi.h"
#include <QtSql>
sindesi::sindesi(QWidget *parent,QSqlDatabase db)
: QDialog(parent)
{
db5= db;// here i pass and copy the whole object and via the db5 the db is still accesible
ui.setupUi(this);
connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
}
void sindesi::sindesou() {
if (!db5.isOpen()) {
db5.setDatabaseName(ui.pinakas->text());
db5.setUserName(ui.onoma->text());
db5.setPassword(ui.kodikos->text());
db5.setHostName("127.0.0.1");
db5.setPort(3306);
if (!db5.open()) {
ui.katastasi->setText("not connected");
} else {
ui.katastasi->setText("connected");
}
} else {
ui.katastasi->setText("it has already been connected");
}
}
and the header:
#ifndef SINDESI_H
#define SINDESI_H
#include <QtSql>
#include <QtGui/QDialog>
#include "ui_sindesi.h"
class sindesi : public QDialog
{
Q_OBJECT
public:
sindesi(QWidget *parent,QSqlDatabase db);
~sindesi();
QSqlDatabase db5;
public slots:
void sindesou();
void aposindesou();
private:
Ui::sindesiClass ui;
};
To copy to clipboard, switch view to plain text mode
I am not sure if that supposed to be correct but in both cases works exactly the same.
Many thanks in advance.
Regards
Bookmarks