Results 1 to 3 of 3

Thread: inherit from QSqlDatabase

  1. #1
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question inherit from QSqlDatabase

    Good afternoon,

    I guess it is more a c++ beginner's question than actually Qt releated. So it probably fits best here:

    I want to access a MySql database. Everything is working fine. To get a better structure I wanted to have my database interactions in one class:

    MyDatabase

    Ok, so why not inherit from QSqlDatabase and extend with the needed additional members:

    Qt Code:
    1. #ifndef MYDATABASE_H
    2. #define MYDATABASE_H
    3.  
    4. #include <QSqlDatabase>
    5.  
    6. class MyDatabase : public QSqlDatabase
    7. {
    8.  
    9. public:
    10. MyDatabase();
    11. };
    12.  
    13. #endif // MYDATABASE_H
    To copy to clipboard, switch view to plain text mode 

    If I now want to create a connection with:

    Qt Code:
    1. mydb = MyDatabase::addDatabase("QMYSQL")
    To copy to clipboard, switch view to plain text mode 

    I get the message:

    Qt Code:
    1. ./Stromverbrauch/mainwindow.cpp: In member function 'int MainWindow::connectDatabase()':
    2. ../Stromverbrauch/mainwindow.cpp:33:44: error: no match for 'operator=' in '((MainWindow*)this)->MainWindow::mydb = QSqlDatabase::addDatabase(const QString&, const QString&)((* & QString((*(const QLatin1String*)(& QLatin1String(QSqlDatabase::defaultConnection))))))'
    3. ../Stromverbrauch/mainwindow.cpp:33:44: note: candidate is:
    4. In file included from ../Stromverbrauch/mainwindow.h:25:0,
    5. from ../Stromverbrauch/mainwindow.cpp:1:
    6. ../Stromverbrauch/mydatabase.h:6:7: note: MyDatabase& MyDatabase::operator=(const MyDatabase&)
    7. ../Stromverbrauch/mydatabase.h:6:7: note: no known conversion for argument 1 from 'QSqlDatabase' to 'const MyDatabase&'
    To copy to clipboard, switch view to plain text mode 

    Ok, I have read somewhere in a different forum the advice to reimplement the "addDatabase" function.
    But why is this necessary? Shoud the child not inheric also static functions?
    What am I missing to understand the issue?

    I am looking forward for your advice.

    RogerWilco77

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: inherit from QSqlDatabase

    It's probably a bad idea at all to try and subclass QSqlDatabase. It is much better and easier to create your own class that accepts QSqlDatabase as a parameter, stores it as a private member and uses it to operate on the database where required.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    RogerWilco77 (27th January 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: inherit from QSqlDatabase

    Thanks for the advice.
    I will follow it. Still it seems like I have something not understood regarding static functions.
    Have a nice weekend.

Similar Threads

  1. Replies: 0
    Last Post: 2nd December 2011, 20:52
  2. How can we inherit ui?
    By vinayaka in forum Newbie
    Replies: 5
    Last Post: 30th May 2011, 15:28
  3. About inherit
    By nesson in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2011, 14:07
  4. How to use the ui_*.h,inherit it or as a member?
    By 75543255 in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2009, 11:45
  5. Inherit the QAxWidget
    By MrShahi in forum Qt Programming
    Replies: 3
    Last Post: 29th April 2008, 09:06

Tags for this Thread

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.