MainWindow 是否在 qt5 中用自身初始化? [英] Is MainWindow initialized with itself in qt5?

查看:47
本文介绍了MainWindow 是否在 qt5 中用自身初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试学习 qt 5,但不明白一件事.Qt creator 默认制作这两个文件:

I try to learn qt 5, but dont understand one thing. Qt creator makes these two files by default:

ma​​inwindow.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

ma​​inwindow.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;
}

我不明白:ui(new Ui::MainWindow) 在构造函数中?我知道它会初始化 ui 指针,但是要初始化什么呢?对自己?所以基本上,这是否意味着 MainWindow 是用自身初始化的,或者可能引用了 MainWindow 的其他实例?如果是这样,它是某种 C++ 编程模式或方法吗?它有名字吗,所以我可以自己阅读.

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.

非常感谢您的解释.

推荐答案

它不是 MainWindow,它是 Ui::MainWindow - 不是同一个类.Ui 命名空间中的类是由 qmake(和朋友)自动生成的类.此类包含初始化并允许您访问表单上的小部件的代码 - 您在图形 Qt 设计器中创建的小部件.

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.

此类包含在您的 cpp 文件第二行的文件中:

This class is contained in file included in second line in your cpp file:

#include "ui_mainwindow.h"

这篇关于MainWindow 是否在 qt5 中用自身初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆