Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly add a .h file to a project?

I want to make it so that when I click on a button from the main menu, a new form opens (not as a dialog, but is replaced with a new form). I created the history.ui form. I also created history.hpp and history.cpp. I’m getting errors in history.cpp.

history.hpp

#ifndef HISTORY_HPP
#define HISTORY_HPP

#include <QDialog>

namespace Ui {
class History;
}

class History : public QDialog
{
    Q_OBJECT

public:
    explicit History(QWidget *parent = nullptr);
    ~History();

private:
    Ui::History *ui;
};

#endif // HISTORY_HPP

mainwindow.hpp

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>
#include "history.hpp"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;
    History *historyDialog;
};

#endif // MAINWINDOW_HPP

history.cpp

#include "history.hpp"
#include "ui_history.h"

History::History(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::History)
{
    ui->setupUi(this);
}

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

enter image description here
I tried rebuilding the project, but I’m still getting errors.

I added all the files to CMakeLists.txt. ChatGPT says that the ui_history.h file is generated automatically.

Ubuntu 24.04 LTS.
QT Creator 13.02.

>Solution :

You’ve declared two different classes in your history.hpp file. The first is a forward declaration for Ui::History. The other is for a class named History at global namespace scope. So when you try to say new Ui::History in your history.cpp, there’s no actual declaration for the compiler to reference.

You probably want this:

#ifndef HISTORY_HPP
#define HISTORY_HPP

#include <QDialog>

namespace Ui {

    class History : public QDialog
    {
        Q_OBJECT

    public:
        explicit History(QWidget *parent = nullptr);
        ~History();

    private:
        History *ui;
    };
}

Or you could just remove that namespace Ui wrapper entirely and stay away from namespaces.

The above declares History within the Ui namespace. No forward declaration is needed.

You have the same mistake in mainwindow.hpp as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading