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
#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.