问题
I try to learn qt 5, but dont understand one thing. Qt creator makes these two files by default:
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) // <!-- what does it do?
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
I dont understand this: ui(new Ui::MainWindow)
in constructor? I know it initializes ui pointer, but to what? To itself? So basically, does it mean that MainWindow
is initialized with itself, or maybe has reference to other instance of MainWindow
? If so, is it some c++ programming pattern or methodology? Does it have a name, so I could read about it myself.
Many thanks in advance for explanation.
回答1:
It's not MainWindow
, it's Ui::MainWindow
- not the same class. Classes in Ui
namespace are classes automatically generated by qmake (and friends). This class contains code that initializes and allows you access to widgets on your form - ones you created in graphical Qt designer.
This class is contained in file included in second line in your cpp file:
#include "ui_mainwindow.h"
来源:https://stackoverflow.com/questions/28016303/is-mainwindow-initialized-with-itself-in-qt5