I am learning Qt, Qt 5. When I start Qt Creator and create a project with ALL the default settings, I get these 2 files generated, (I am excluding main.cpp and the .pro file)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
Now, I prefer to do it this way,
my_mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow, private Ui_MainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
};
#endif // MAINWINDOW_H
my_mainwindow.cpp
#include "my_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);
}
MainWindow::~MainWindow()
{
}
Here are the main differences between my code and Qt Creator's code:
- No
namespace Ui
in my code. (Could anyone explain me the use of this namespace here ?) - I inherit the
MainWindow
class from bothQMainWindow
andUi_MainWindow
whereas the Qt Creator's code inherits it only from theQMainWindow
class.
My question is, is there ANY disadvantage of using my approach or is there any advantage of using the Qt Creator's approach ? Please give a detailed answer.