Results 1 to 4 of 4

Thread: Using Sub Classes (I think that is what im trying to do)

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Using Sub Classes (I think that is what im trying to do)

    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:

    Qt Code:
    1. createDatabaseConn();
    To copy to clipboard, switch view to plain text mode 

    and the function is like so

    Qt Code:
    1. bool createDatabaseConn()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("myhostname");
    5. db.setDatabaseName("mydatabasename");
    6. db.setUserName("myusername");
    7. db.setPassword("mypassword");
    8. if (!db.open()) {
    9. //QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    10. return false;
    11. }
    12. return true;
    13. }
    To copy to clipboard, switch view to plain text mode 

    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 :

    Qt Code:
    1. #ifndef MYDATABASE_H
    2. #define MYDATABASE_H
    3.  
    4. #include <QObject>
    5. #include <QSettings>
    6. #include <QSqlDatabase>
    7.  
    8. class myDatabase : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit myDatabase(QObject *parent = 0);
    13.  
    14. signals:
    15.  
    16. public slots:
    17. bool createDatabaseConn();
    18.  
    19. };
    20.  
    21. #endif // MYDATABASE_H
    To copy to clipboard, switch view to plain text mode 

    in my database.cpp
    Qt Code:
    1. #include "mydatabase.h"
    2.  
    3.  
    4. myDatabase::myDatabase(QObject *parent) :
    5. QObject(parent)
    6. {
    7. }
    8.  
    9.  
    10. bool createDatabaseConn()
    11. {
    12. QSettings settings("ATSTech", "mysettings");
    13.  
    14. settings.beginGroup("database");
    15.  
    16. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    17.  
    18. db.setHostName(settings.value("server").toString());
    19. db.setDatabaseName("atsadmin");
    20. db.setUserName(settings.value("databaseUsername").toString());
    21. db.setPassword(settings.value("databasePassword").toString());
    22.  
    23. settings.endGroup();
    24.  
    25. if (!db.open()) {
    26. //QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    27. return false;
    28. }
    29. return true;
    30. }
    To copy to clipboard, switch view to plain text mode 

    So now i thought using the connection would be as simple as


    Qt Code:
    1. #include "mydatabase.h"
    2.  
    3. rm_customer_search::frm_customer_search(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::frm_customer_search)
    6. {
    7. ui->setupUi(this);
    8.  
    9. myDatabase::createDatabaseConn();
    10. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using Sub Classes (I think that is what im trying to do)

    Quote Originally Posted by ShapeShiftme View Post
    frm_customer_search.cpp:10: error: cannot call member function ‘bool myDatabase::createDatabaseConn()’ without object
    This error message does explain the problem very clearly.
    You need to create an object first.

    Example:
    Qt Code:
    1. myDatabase myDb;
    2. myDb.createDatabaseConn();
    3.  
    4. // or
    5. myDatabase *myDb = new myDatabase();
    6. myDb->createDatabaseConn();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to tbscope for this useful post:

    ShapeShiftme (16th January 2011)

  4. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using Sub Classes (I think that is what im trying to do)

    Create -in your MainWindow- only one connection with the database and pass it to every dialog that has to use it.
    Something like this:
    Qt Code:
    1. db->addDatabase("QMYSQL");
    2. db->...etc...
    3.  
    4. ...
    5. myDialog = new MyDialog(this, db);
    6. myDialog->exec();
    7. ...
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Using Sub Classes (I think that is what im trying to do)

    Quote Originally Posted by tbscope View Post
    This error message does explain the problem very clearly.
    You need to create an object first.

    Example:
    Qt Code:
    1. myDatabase myDb;
    2. myDb.createDatabaseConn();
    3.  
    4. // or
    5. myDatabase *myDb = new myDatabase();
    6. myDb->createDatabaseConn();
    To copy to clipboard, switch view to plain text mode 
    Thnaks very much. Very obvious when someone tells you what you are doing wrong.
    And works great

Similar Threads

  1. QT WMI Classes
    By justatiq in forum Qt Programming
    Replies: 31
    Last Post: 10th April 2011, 15:44
  2. construction of classes within classes
    By mobucl in forum Newbie
    Replies: 8
    Last Post: 10th January 2011, 13:51
  3. Using WMI CLasses
    By newb in forum Newbie
    Replies: 1
    Last Post: 4th June 2010, 19:19
  4. fax classes in qt
    By dyams in forum Qt Programming
    Replies: 5
    Last Post: 7th September 2007, 09:14

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.