Results 1 to 14 of 14

Thread: cannot share the database connection!!!!

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.