如何使用Qt Creator将C ++头文件添加到Qt项目中

时间:2016-03-31 13:16:41

标签: c++ mysql qt

我想开发一个小型Qt Widget应用程序,它可以从MySQL数据库中获取绘图数据。我有一些我想在C ++头文件中使用的函数,我为旧的C ++项目编写。我想使用此头文件中的函数,但我无法将此标头正确添加到我的Qt项目中。

在Qt Creator中,我使用“添加现有文件”对话框添加头文件,我还告诉Qt Creator使用我的头文件中使用的MySQL_connector C ++库。 .pro文件现在看起来像这样:

#-------------------------------------------------
#
# Project created by QtCreator 2016-03-31T14:12:08
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = dummy
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    Level2Snapshot.cpp

HEADERS  += mainwindow.h \
    Level2Snapshot.h \
    MySQL_Operations.h

FORMS    += mainwindow.ui

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lmysqlcppconn

INCLUDEPATH += $$PWD/../../../../usr/include
DEPENDPATH += $$PWD/../../../../usr/include

现在,当我想在MainWindow源文件中初始化必要的sql对象时,编译过程中出现“未声明”错误。

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sql::Driver *driver;
    sql::Connection *con;
}

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

我可以通过将包含所有MySQL相关内容的“MySQL_Operations.h”包含在源文件中来实现它的工作:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "MySQL_Operations.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sql::Driver *driver;
    sql::Connection *con;
}

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

但这对我没有帮助,因为我需要在我的MainWindow.h中包含头文件,因为我想在那里声明方法,以MySQL Connection作为参数。像这样的例如:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "MySQL_Operations.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void print_time_span(sql::Connection *con, std::string database, std::string table);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

这样做,我得到MySQL_Operations.h中实现的所有功能的“多重定义”错误。我知道在多重定义问题上已经提出了类似的问题,但没有任何帮助。显然编译器知道我的头文件的内容,因为否则我不明白它如何抱怨多个定义。但是没有明确地将MySQL_Operations.h添加到#include语句中,而只是添加到.pro文件中,在使用Qt Creator创建的Qt项目中如何完成它不起作用。

我对我头文件中的MySQL内容付出了很多努力,我真的很想使用它,而不是Qt提供的MySQL工具。因此,非常感谢任何帮助。我是Qt的新手,可能不知道有关构建过程的一些基本知识。所以,请不要犹豫,向我提出最简单的建议。

非常感谢! 尼尔斯

感谢目前为止的答案。我将标题减少到最小,它仍然重现错误。这是文件:

#ifndef MYSQL_OPERATIONS_H
#define MYSQL_OPERATIONS_H

#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <vector>

namespace dom{


    void print_time_span(sql::Connection *con, std::string database, std::string table){
        sql::Statement *stmt;
        sql::ResultSet *res;

        std::string use_database = "USE " + database;

        stmt = con->createStatement();
        stmt->execute(use_database);

        res = stmt->executeQuery("SELECT MAX(Time) AS Time FROM test");
        res->next();   

        std::string endtime = res->getString("Time");

        res = stmt->executeQuery("SELECT MIN(Time) AS Time FROM test");
        res->next();   

        std::string starttime = res->getString("Time");
        std::cout << "First entry in " <<
                database <<
                "." <<
                table << 
                " is from " << starttime << 
                " and ranges to " << 
                endtime << "." << std::endl;


        delete res;
        delete stmt;
    }

}
#endif /* MYSQL_OPERATIONS_H */

这只是一个简单的查询。在MainWindow.h中包含头文件时添加的错误是:

/home/niels/Programmierung/dummy/MySQL_Operations.h:28: Fehler: multiple definition of `dom::print_time_span(sql::Connection*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

我两次得到这个错误。

1 个答案:

答案 0 :(得分:0)

实际帮助我的是这篇文章:https://stackoverflow.com/a/20679534/4583985

使命名空间名称减去&#34;多个定义&#34;错误消失了。也许有人可以解释实际上做了什么。

最佳

尼尔斯