简体   繁体   English

如何在Qt中将数据库类继承到MainWindow

[英]How to inherit database class to MainWindow in Qt

I am trying to establish a database connection in Qt using OOP concepts. 我正在尝试使用OOP概念在Qt中建立数据库连接。 I have a separate databaseconnection class and databaseconnection header file. 我有一个单独的databaseconnection类和databaseconnection头文件。

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

This Login.h file 此Login.h文件

#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 (). 这是实现dbcon()的我的databaseconnection.cpp。

#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. 我想在MainWindow中调用此数据库连接方法,而不是在MainWindow中实现连接。 This is my MainWindow class. 这是我的MainWindow类。

#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. 尽管我包括了所有头文件,但它给出了一个错误,指出dbcon()超出范围。 I don't have an idea how to inherit databaseconnection class in MainWindow class(MainWindow class is the login.cpp) 我不知道如何继承MainWindow类中的databaseconnection类(MainWindow类是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 您的databaseconnection.h声明了一个独立的函数dbcon()但是在实现文件(cpp)中,您正在定义一个类DatabaseConection,其中的dbcon()是方法

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. 您需要在标头(构造函数等)中正确声明DatabaseConnection类,然后在MainWindow中可以实例化该类型的类并使用该方法。

You are doing it wrong. 你做错了。 Try this 尝试这个

databaseconnection.h 数据库连接

#ifndef DATABASECONNECTION  
#define DATABASECONNECTION

class DatabaseConnection
{
  public:
   DatabaseConnection() {}

   bool dbcon() const;
};

#endif //DATABASECONNECTION

databaseconnection.cpp 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.cpp

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

    dbcon();
}
  1. Composition (preferred - see C++ coding standards book) 组成(首选-参见C ++编码标准书)

login.h 登录名

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

   private:
    DatabaseConnection dbConnection;
};

login.cpp login.cpp

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

    dbConnection.dbcon();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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