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

Answers


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();
}

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.


Need Your Help

Facebook login dialog not working in IE10 mobile

internet-explorer facebook-javascript-sdk

I am new to facebook development. I have a mobile app that I need to connect to facebook. I am using Facebook JavaScript SDK to do the authentication. Well its working fine on desktop browsers and ...

How to check out with Batch , if a folder contains a .txt file and then copy the folder into an other location?

windows batch-file

I would like to make a script with Batch. I want from this script to check it out, If a folder contains a file, "list.txt", and if it is in the folder i want to make a copy in an other location. I ...