Results 1 to 14 of 14

Thread: cannot share the database connection!!!!

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default cannot share the database connection!!!!

    Hi all.

    I just made the next step to start making applications that make sense but it seems that i get confused easy.

    I have a main window(kentriko class). I have a dialogue used to initiate a database connection (class database). This dialogue is called from the mainwindow to create the database connection. The dialogue return the database ( *d.get_db() ).

    I want to use this connection to make queries from another form called kentriko (kentriko class). This class is subwindowed. This allows me to instate many instances of this window all of the using the same database.

    The problem is that I am making a mistake in the constructors somehow and the pointers to the database are not passed. the code is listed bellow.

    Any help will be much appreciated.

    parathiro implementation:
    Qt Code:
    1. #include "parathiro.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include "../diafores_sinartisis/sinartisis.h"
    5. #include "../database/database.h"
    6.  
    7. using namespace std;
    8. parathiro::parathiro(QWidget *parent,database d)//<-- here i pass the oject (the database connection dialog)//
    9. : QMainWindow(parent)
    10. {
    11. ui.setupUi(this);
    12. connect(ui.anixe, SIGNAL(clicked()) , this , SLOT(anixe()));
    13. connect(ui.pare, SIGNAL(clicked()) , this , SLOT(pare()));
    14. connect(ui.proto,SIGNAL(textChanged(const QString&)),this,SLOT(alagi()));
    15. }
    16.  
    17. void parathiro::alagi(){
    18. QSqlQuery erotisi(*d.get_db());//<- here i attempt to use it//
    19. proto = sinartisi_SELECT( "onoma,epitheto" ,"onomata", "onoma", ui.proto->text());
    20. ui.deytero->setText(proto);
    21. erotisi.exec(proto);
    22. variant = erotisi.size();
    23. ui.trito->setText(variant.toString());
    24. }
    To copy to clipboard, switch view to plain text mode 

    parathiro header:

    Qt Code:
    1. #ifndef PARATHIRO_H
    2. #define PARATHIRO_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_parathiro.h"
    6. #include "../database/database.h"
    7.  
    8. class parathiro : public QMainWindow
    9. {
    10. Q_OBJECT
    11. public:
    12. parathiro(QWidget *parent = 0, database d );
    13. ~parathiro();
    14. private slots:
    15. void anixe();
    16. void pare();
    17. void alagi();
    18. private:
    19. Ui::parathiroClass ui;
    20. QString proto;
    21. QString deytero;
    22. QVariant variant;
    23. };
    To copy to clipboard, switch view to plain text mode 

    kentriko implementation:
    Qt Code:
    1. #include "kentriko.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include "../diafores_sinartisis/sinartisis.h"
    5. #include <QMdiArea>
    6. #include <QMdiSubWindow>
    7. #include "../kentriko/parathiro/parathiro.h"
    8.  
    9. using namespace std;
    10.  
    11. kentriko::kentriko(QWidget *parent)
    12. : QMainWindow(parent)
    13. {
    14. ui.setupUi(this);
    15. connect(ui.neo_parathiro, SIGNAL(triggered()), this, SLOT(anixe()));
    16. connect(ui.sindesi,SIGNAL(triggered()),this,SLOT(sindesi()));
    17. }
    18. void kentriko::anixe(){
    19. parathiro *para = new parathiro(0,d);//<- here i am trying to pass it to the new parathiro//
    20. QMdiSubWindow *subwindow = ui.mdiArea->addSubWindow(para);
    21. subwindow->setAttribute(Qt::WA_DeleteOnClose);
    22. subwindow->resize(sizeHint());
    23. subwindow->show();
    24. }
    25.  
    26. void kentriko::sindesi(){
    27. database* d;//<<<<- here i am creating the pointer to the database class//
    28. d->show();
    29. }
    To copy to clipboard, switch view to plain text mode 

    database implementation:
    Qt Code:
    1. #include "database.h"
    2.  
    3. database::database(QWidget *parent) :
    4. QDialog(parent) {
    5. ui.setupUi(this);
    6. db = QSqlDatabase::addDatabase("QMYSQL");
    7. connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
    8. connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
    9. connect(ui.pliroforia_k, SIGNAL(clicked()), this, SLOT(pliroforia()));
    10. }
    11. void database::sindesou() {
    12. ena.pinakas = ui.pinakas->text();
    13. ena.onoma = ui.onoma->text();
    14. ena.kodikos = ui.kodikos->text();
    15. if (!db.isOpen()) {
    16. db.setDatabaseName(ena.pinakas);
    17. db.setUserName(ena.onoma);
    18. db.setPassword(ena.kodikos);
    19. db.setHostName("127.0.0.1");
    20. db.setPort( 3306);
    21. if (!db.open()) {
    22. ui.katastasi->setText("Not connected");
    23. } else {
    24. ui.katastasi->setText("connected");
    25. }
    26. } else {
    27. ui.katastasi->setText("It has already been connected");
    28. }
    29. }
    30. void database::aposindesou() {
    31. db.close();
    32. }
    33. void database::pliroforia() {
    34. ena.pliroforia = ui.pliroforia->text();
    35. }
    36. A * database::get_alpha() {
    37. return &ena;
    38. }
    39. QSqlDatabase * database::get_db() {
    40. return &db;//<<<<---- here i am returning the database connection from the database class in order to use it on the parathiro.
    41. }
    42. database::~database() {
    43. db.close();
    44. }
    To copy to clipboard, switch view to plain text mode 

    i have attached the code as complete files.

    Many thanks in advance.
    Attached Files Attached Files
    Last edited by cbarmpar; 22nd September 2008 at 21:09.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!

    where is new operator in your code?
    Qt Code:
    1. void kentriko::sindesi(){
    2. database* d;//<<<<- here i am creating the pointer to the database class//
    3. d->show();
    4. }
    To copy to clipboard, switch view to plain text mode 
    why you don't use ref or pointer to database instead of copying it?
    Qt Code:
    1. parathiro::parathiro(QWidget *parent,database d)//<-- here i pass the oject (the database connection dialog)//
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!


  4. #4
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cannot share the database connection!!!!

    Thank you for the reply but still not aware on what should I change.

    Why and where should I use the new operator?

    Many thanks in advance.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!

    in this method
    Qt Code:
    1. void kentriko::sindesi(){
    2. database* d;//<<<<- here i am creating the pointer to the database class//
    3. d->show();
    4. }
    To copy to clipboard, switch view to plain text mode 
    so, it should look like
    Qt Code:
    1. void kentriko::sindesi(){
    2. database* d = new database();
    3. d->show();
    4. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cannot share the database connection!!!!

    thank you for the reply.

    Shall i leave the constractor as it is?

    Since d is a pointer i am not coping the object right when i do the following:

    parathiro *para = new parathiro(0,d);

    do i also need to change the constractor of the parathiro?

    Regards.

  7. #7
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cannot share the database connection!!!!

    sorry forgot to add:

    my implementation now looks like:
    Qt Code:
    1. parathiro::parathiro(QWidget *parent = 0, database &d)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5. connect(ui.anixe, SIGNAL(clicked()) , this , SLOT(anixe()));
    6. connect(ui.pare, SIGNAL(clicked()) , this , SLOT(pare()));
    7. connect(ui.proto,SIGNAL(textChanged(const QString&)),this,SLOT(alagi()));
    8. }
    9.  
    10. void parathiro::alagi(){
    11. QSqlQuery erotisi(*d->get_db());
    12. proto = sinartisi_SELECT( "onoma,epitheto" ,"onomata", "onoma", ui.proto->text());
    13. ui.deytero->setText(proto);
    14. erotisi.exec(proto);
    15. variant = erotisi.size();
    16. ui.trito->setText(variant.toString());
    17. }
    To copy to clipboard, switch view to plain text mode 


    and this is how i call it:
    Qt Code:
    1. void kentriko::anixe(){
    2. parathiro *para = new parathiro(d);
    3. QMdiSubWindow *subwindow = ui.mdiArea->addSubWindow(para);
    4. subwindow->setAttribute(Qt::WA_DeleteOnClose);
    5. subwindow->resize(sizeHint());
    6. subwindow->show();
    7. }
    8.  
    9. void kentriko::sindesi(){
    10. database *d = new database;
    11. d->show();
    12. }
    To copy to clipboard, switch view to plain text mode 

    regards.

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!

    of course.
    so it should look like
    Qt Code:
    1. class parathiro : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. parathiro(QWidget *parent = 0, database *d);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cannot share the database connection!!!!

    still cant compile i am getting an error in the constructor of parathrio.

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!

    what kind of error do you get?

  11. #11
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cannot share the database connection!!!!

    in the parathiro.h:
    Qt Code:
    1. #ifndef PARATHIRO_H
    2. #define PARATHIRO_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_parathiro.h"
    6. #include "../database/database.h"
    7. database *d;
    8. class parathiro : public QMainWindow
    9. {
    10. Q_OBJECT
    11. public:
    12. parathiro(QWidget *parent = 0,database *d = 0);//err= after previous specification in parathiro::parathiro(QWidget*.database*)
    13. ~parathiro();
    To copy to clipboard, switch view to plain text mode 

    and in the parathiro.cpp:
    Qt Code:
    1. #include "parathiro.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include "../diafores_sinartisis/sinartisis.h"
    5. #include "../database/database.h"
    6. #include <QMainWindow>
    7. using namespace std;
    8.  
    9. parathiro::parathiro(QWidget *parent = 0, database *d)//Multiple markers at this line
    10. // - default argument given for parameter 1 of
    11. // ‘parathiro::parathiro(QWidget*, database*)’
    12. // - unused parameter ‘d’
    13. : QMainWindow(parent)
    14. {
    15. ui.setupUi(this);
    16. connect(ui.anixe, SIGNAL(clicked()) , this , SLOT(anixe()));
    17. connect(ui.pare, SIGNAL(clicked()) , this , SLOT(pare()));
    18. connect(ui.proto,SIGNAL(textChanged(const QString&)),this,SLOT(alagi()));
    19. }
    20.  
    21. void parathiro::alagi(){
    22. QSqlQuery erotisi(d->get_db());// no matching function to call 'Qsqlquery::QSqlquery(QSqlDatabase)
    23. proto = sinartisi_SELECT( "onoma,epitheto" ,"onomata", "onoma", ui.proto->text());
    24. ui.deytero->setText(proto);
    25. erotisi.exec(proto);
    26. variant = erotisi.size();
    27. ui.trito->setText(variant.toString());
    28. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!

    the wrong code in cpp file
    Qt Code:
    1. ...
    2. parathiro::parathiro(QWidget *parent = 0, database *d)
    3. ...
    To copy to clipboard, switch view to plain text mode 

    must be
    Qt Code:
    1. ...
    2. parathiro::parathiro(QWidget *parent, database *d)
    3. ...
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot share the database connection!!!!

    actually, I suggest you to use QSqlDatabase::database and refuse to use pointer to database object.

  14. #14
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cannot share the database connection!!!!

    i tried almost everything and it doesnt seem to work. Additionaly the eclipse IDE seems to stuck sometimes and i need to clean the project. most of the times even when i made the correct change the moc files get stuck andt.I see now why people dont prefer linux

    I have attached the implementation together with the errors i recieve:

    Qt Code:
    1. #include "kentriko.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include "../diafores_sinartisis/sinartisis.h"
    5. #include <QMdiArea>
    6. #include <QMdiSubWindow>
    7. #include "../kentriko/parathiro/parathiro.h"
    8. using namespace std;
    9.  
    10. kentriko::kentriko(QWidget *parent)
    11. : QMainWindow(parent)
    12. {
    13. ui.setupUi(this);
    14. connect(ui.neo_parathiro, SIGNAL(triggered()), this, SLOT(anixe()));
    15. connect(ui.sindesi,SIGNAL(triggered()),this,SLOT(sindesi()));
    16. }
    17. void kentriko::anixe(){
    18. parathiro *para = new parathiro(0,d);
    19. QMdiSubWindow *subwindow = ui.mdiArea->addSubWindow(para);
    20. subwindow->setAttribute(Qt::WA_DeleteOnClose);
    21. subwindow->resize(sizeHint());
    22. subwindow->show();
    23. }
    24.  
    25. void kentriko::sindesi(){
    26. database *d = new database;
    27. d->show();
    28. }
    To copy to clipboard, switch view to plain text mode 

    the parathiro implementation:

    Qt Code:
    1. #include "parathiro.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include "../diafores_sinartisis/sinartisis.h"
    5. #include "../database/database.h"
    6. #include <QMainWindow>
    7. using namespace std;
    8.  
    9. parathiro::parathiro(QWidget *parent, database *d)//<--- unsued parameter 'd'
    10. : QMainWindow(parent)
    11. {
    12. ui.setupUi(this);
    13. connect(ui.anixe, SIGNAL(clicked()) , this , SLOT(anixe()));
    14. connect(ui.pare, SIGNAL(clicked()) , this , SLOT(pare()));
    15. connect(ui.proto,SIGNAL(textChanged(const QString&)),this,SLOT(alagi()));
    16. }
    17.  
    18. void parathiro::alagi(){
    19. QSqlQuery erotisi( *d->get_db());
    20. proto = sinartisi_SELECT( "onoma,epitheto" ,"onomata", "onoma", ui.proto->text());
    21. ui.deytero->setText(proto);
    22. erotisi.exec(proto);
    23. variant = erotisi.size();
    24. ui.trito->setText(variant.toString());
    25. }
    To copy to clipboard, switch view to plain text mode 

    and finally the database implementation:

    Qt Code:
    1. #include "database.h"
    2.  
    3. database::database(QWidget *parent) :
    4. QDialog(parent) {
    5. ui.setupUi(this);
    6. db = QSqlDatabase::addDatabase("QMYSQL");
    7. connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
    8. connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
    9. connect(ui.pliroforia_k, SIGNAL(clicked()), this, SLOT(pliroforia()));
    10. }
    11. void database::sindesou() {
    12. ena.pinakas = ui.pinakas->text();
    13. ena.onoma = ui.onoma->text();
    14. ena.kodikos = ui.kodikos->text();
    15. if (!db.isOpen()) {
    16. db.setDatabaseName(ena.pinakas);
    17. db.setUserName(ena.onoma);
    18. db.setPassword(ena.kodikos);
    19. db.setHostName("127.0.0.1");
    20. db.setPort( 3306);
    21. if (!db.open()) {
    22. ui.katastasi->setText("Not connected");
    23. } else {
    24. ui.katastasi->setText("connected");
    25. }
    26. } else {
    27. ui.katastasi->setText("It has already been connected");
    28. }
    29. }
    30.  
    31. ..............
    32. QSqlDatabase * database::get_db() {
    33. return &db;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks for your support so far.

    Regards.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. database connection
    By peace_comp in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2008, 12:16
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.