简体   繁体   中英

How to inherit database class to MainWindow in Qt

I am trying to establish a database connection in Qt using OOP concepts. I have a separate databaseconnection class and databaseconnection header file.

  #ifndef DATABASECONNECTION  
  #define DATABASECONNECTION
  bool dbcon();
  #endif // DATABASECONNECTION

This Login.h file

#include <QFileInfo>

namespace Ui {
class Login;
}

class Login : public QMainWindow
{
    Q_OBJECT

public:
    explicit Login(QWidget *parent = 0);
    ~Login();

private slots:
   // void on_btnLogin_clicked();

    void on_btnCancel_clicked();

    void on_btnLog_clicked();

private:
    Ui::Login *ui;
};

#endif // LOGIN_H

.This is my databaseconnection.cpp which implements the dbcon ().

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>

#include "databaseconnection.h"

DatabaseConection::DatabaseConection()
{

}
bool DatabaseConection :: dbCon()
{
QSqlDatabase db = QSqlDatabase :: addDatabase("QMSQL");
db.setHostName("localhost");
db.setDatabaseName("library");
db.setUserName("root");
db.setPassword("");
if(!db.open())
{
  qDebug()<<"Database error occured";
return false;

}
else
    return true;
}

I want to call this database connection method inside my MainWindow instead of implementing the connection within MainWindow. This is my MainWindow class.

#include <QtSql>

#include <QDebug>
#include <QFileInfo>
#include "login.h"
#include "databaseconection.h"
#include "ui_login.h"


Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    dbcon();



}

Login::~Login()
{
    delete ui;
}



void Login::on_btnLog_clicked()
{


}

void Login::on_btnCancel_clicked()
{

}

Although I include all the header files It gives an error saying that dbcon() is out of scope. I don't have an idea how to inherit databaseconnection class in MainWindow class(MainWindow class is the login.cpp)

After performing some actions and trying to run the project this was shown. I can't figure out the reason for this. Please help me to figure out this. Thanks in advance

Starting H:\c++Prac\build-Libro-Desktop_Qt_5_5_1_MinGW_32bit-Debug\debug\Libro.exe...
The program has unexpectedly finished.
H:\c++Prac\build-Libro-Desktop_Qt_5_5_1_MinGW_32bit-Debug\debug\Libro.exe crashed

Your databaseconnection.h declares a standalone function dbcon() but in the implementation file (the cpp) you're defining a class DatabaseConection of which dbcon() is a method

You need to properly declare the DatabaseConnection class in the header (constructor etc.), then in MainWindow you can instantiate a class of that type and use the method.

You are doing it wrong. Try this

databaseconnection.h

#ifndef DATABASECONNECTION  
#define DATABASECONNECTION

class DatabaseConnection
{
  public:
   DatabaseConnection() {}

   bool dbcon() const;
};

#endif //DATABASECONNECTION

databaseconnection.cpp

bool DatabaseConnection::dbcon() const
{
  QSqlDatabase db = QSqlDatabase :: addDatabase("QMSQL");
  db.setHostName("localhost");
  db.setDatabaseName("library");
  db.setUserName("root");
  db.setPassword("");
  if(!db.open()) {
    qDebug()<<"Database error occured";
    return false;
  } else {
    return true;
  }
}

Now, you have two options.

  1. Inheritance

login.h

#include "databaseconnection.h"
class Login : public QMainWindow, public DatabaseConnection
{
    Q_OBJECT
   ...
};

login.cpp

Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    dbcon();
}
  1. Composition (preferred - see C++ coding standards book)

login.h

#include "databaseconnection.h"
class Login : public QMainWindow
{
    Q_OBJECT
   ...

   private:
    DatabaseConnection dbConnection;
};

login.cpp

Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login),
    dbConnection()
{
    ui->setupUi(this);

    dbConnection.dbcon();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM