Is MainWindow initialized with itself in qt5?

爷,独闯天下 提交于 2019-12-21 06:19:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!