Results 1 to 5 of 5

Thread: QSqlDatabase usage

  1. #1
    Join Date
    Jul 2007
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Question QSqlDatabase usage

    I have an application that is using an SQLite database. I am trying to follow Qt examples by connecting via the conneciton.h file, but do not seem to get it.
    I have main.cpp call gui.cpp which creates a stackedWidget with 4 widgets, 2 of these will (may) someday include QTableViews that are simple read-only tables from a database. I have seperated the 4 different widgets into their own classes (which are also in different files - not sure if that is important or not). I am trying to have one of the widget classes query the db and the query returns false.
    Where should I actually put the "createConnection()" call (it's in main.cpp right now), and how do I get a pointer (or something else) to use that my query can use. I think I am thinking too much - cannot see thru the fog .
    -Thanks in advance
    Goes2bob

  2. #2
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSqlDatabase usage

    Where should I actually put the "createConnection()" call (it's in main.cpp right now), and how do I get a pointer (or something else) to use that my query can use. I think I am thinking too much - cannot see thru the fog
    main.cpp is a right place to call createConnection. You can use like this;
    Qt Code:
    1. #include <QApplication>
    2. #include "connection.h"
    3.  
    4.  
    5. int main(int argc, char ** argv)
    6. {
    7. QApplication app( argc, argv );
    8. if(!createConnection())
    9. return app.exec();
    10. ......
    11. ......
    12. ......
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  3. #3
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlDatabase usage

    Quote Originally Posted by goes2bob View Post
    I have main.cpp call gui.cpp which creates a stackedWidget with 4 widgets, 2 of these will (may) someday include QTableViews that are simple read-only tables from a database.
    I'm a beginner too, so may be I am not right, but my main.cpp looks only like this:

    Qt Code:
    1. // main.cpp
    2. #include <QApplication>
    3.  
    4. int main( int argc, char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. Erpel myErpel;
    9. myErpel.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Then I have a file called Erpel.cpp. This is the file where I put the "createConnection()" call.

    Qt Code:
    1. // Erpel.cpp
    2. #include "Erpel.h"
    3.  
    4. Erpel::Erpel(QWidget *parent) : QMainWindow( parent )
    5. {
    6. setupUi(this);
    7.  
    8. stackedWidget -> setCurrentIndex(0);
    9.  
    10. connect( actionHome,SIGNAL (triggered()), this, SLOT(slotHome()) );
    11. connect( actionBackup,SIGNAL (triggered()), this, SLOT(fileBackup()) );
    12. connect( actionRestore,SIGNAL (triggered()), this, SLOT(fileRestore()) );
    13.  
    14. // more code follows here ....
    15. }
    16.  
    17. bool Erpel::dbConnect(QString dbName)
    18. {
    19. QString curDir = QCoreApplication::applicationDirPath();
    20. QString iniFile = curDir+"/erpel.ini"; // get some parameters from erpel.ini
    21. QSettings settings(iniFile, QSettings::IniFormat);
    22.  
    23. QString dbdriver = settings.value("database/dbdriver").toString();
    24. QString dbhost = settings.value("database/dbhost").toString();
    25. QString dbname = settings.value("database/dbname").toString();
    26. QString dbuser = settings.value("database/dbuser").toString();
    27. QString dbpasswd = settings.value("database/dbpasswd").toString();
    28.  
    29. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    30.  
    31. db.setHostName(dbhost);
    32. db.setDatabaseName(dbName);
    33. db.setUserName(dbuser);
    34. db.setPassword(dbpasswd);
    35.  
    36. bool connect2Db = db.open(); // try to open the database ...
    37.  
    38. if (!connect2Db)
    39. {
    40. QMessageBox::critical(0, qApp->tr("ERROR!"),
    41. qApp->trUtf8("I was not able to connect to your database!"),
    42. QMessageBox::Cancel);
    43. return false;
    44. }
    45.  
    46. qDebug() << "dbconnect" << db.isValid();
    47.  
    48. return true;
    49. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. // Erpel.h // not complete, only as a sample ....
    2. #ifndef ERPEL_H
    3. #define ERPEL_H
    4.  
    5. #include <QtGui>
    6. #include <QtCore/QVariant>
    7. #include <QSettings>
    8.  
    9. #include <QtSql/QSqlDatabase>
    10. #include <QtSql/QSqlError>
    11. #include <QtSql/QSqlQuery>
    12. #include <QtSql/QSqlRecord>
    13.  
    14. #include <QDebug>
    15.  
    16. #include "ui_erpel.h"
    17.  
    18. class Erpel : public QMainWindow, public Ui::Erpel
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. Erpel (QWidget * parent = 0);
    24. ~Erpel();
    25.  
    26. QString dbdriver;
    27. QString dbhost;
    28. QString dbname;
    29. QString dbuser;
    30. QString dbpasswd;
    31.  
    32. bool dbConnect(QString dbName);
    33.  
    34. public slots:
    35. void slotClose();
    36.  
    37. private:
    38. void adjustHeader();
    39.  
    40. private slots:
    41.  
    42. void editButton(int name);
    43.  
    44. protected:
    45.  
    46. signals:
    47.  
    48. };
    49. #endif //ERPEL_H
    To copy to clipboard, switch view to plain text mode 

    I think I am thinking too much - cannot see thru the fog .
    I know that feeling very well, but may be the code above will help you a little bit to find your way even when it's foggy ....
    Last edited by gboelter; 10th February 2008 at 03:24. Reason: reformatted to look better

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSqlDatabase usage

    Perhaps you forgot to call Erpel::dbConnect()?
    J-P Nurmi

  5. #5
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlDatabase usage

    Quote Originally Posted by jpn View Post
    Perhaps you forgot to call Erpel::dbConnect()?
    Hello jpn,

    I prefer to call Erpel::dbConnect() when I need some datas from my database, not during starting the program.

    Like this for example:

    Qt Code:
    1. void Erpel::showKasseLayout()
    2. {
    3. if (dbConnect(currentMandant))
    4. {
    5. QSqlQuery query;
    6. QString image;
    7.  
    8. query.prepare("SELECT ID, Beschriftung, ArtikelNummer, Image, ToolTip, TimeStamp FROM kassenlayout");
    9.  
    10. query.exec();
    11.  
    12. if (query.isSelect())
    13. query.first();
    14.  
    15. // more code follows here ....
    To copy to clipboard, switch view to plain text mode 

    I think it's a little bit more code and a little bit more to do for the computer, but it's safer.

    Is that correct?

Similar Threads

  1. Reducing CPU Usage
    By Kapil in forum Qt Programming
    Replies: 8
    Last Post: 3rd April 2011, 14:43
  2. QPersistentModelIndex usage info request
    By defumar in forum Qt Programming
    Replies: 3
    Last Post: 18th January 2008, 15:40
  3. GCC can't find QSqlDatabase!!
    By brevleq in forum Qt Programming
    Replies: 5
    Last Post: 29th October 2007, 07:05
  4. Passing around a QSqlDatabase
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 8th September 2007, 08:41
  5. qsqldatabase does not sense db disconnect
    By rburge in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2006, 18:59

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.