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:
Code:
#ifndef MYDATABASE_H
#define MYDATABASE_H
#include <QSqlDatabase>
{
public:
MyDatabase();
};
#endif // MYDATABASE_H
If I now want to create a connection with:
Code:
mydb = MyDatabase::addDatabase("QMYSQL")
I get the message:
Code:
./Stromverbrauch/mainwindow.cpp: In member function 'int MainWindow::connectDatabase()':
../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))))))'
../Stromverbrauch/mainwindow.cpp:33:44: note: candidate is:
In file included from ../Stromverbrauch/mainwindow.h:25:0,
from ../Stromverbrauch/mainwindow.cpp:1:
../Stromverbrauch/mydatabase.h:6:7: note: MyDatabase& MyDatabase::operator=(const MyDatabase&)
../Stromverbrauch/mydatabase.h:6:7: note: no known conversion for argument 1 from 'QSqlDatabase' to 'const MyDatabase&'
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
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.
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.