Qt的ui->setupUi(this)在做什么?

  • ui->setupUi()

新建好Qt的工程之后,总是会在MainWindow函数中有一行代码

ui->setupUi(this);

跟踪进这行代码

class Ui_MainWindow
{
public:
    QWidget *centralWidget;
    QLabel *label;
    QLabel *label_2;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(635, 550);
        centralWidget = new QWidget(MainWindow);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        label = new QLabel(centralWidget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(70, 90, 491, 321));
        label_2 = new QLabel(centralWidget);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setGeometry(QRect(60, 20, 141, 31));
        MainWindow->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(MainWindow);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 635, 23));
        MainWindow->setMenuBar(menuBar);
        mainToolBar = new QToolBar(MainWindow);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(MainWindow);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        MainWindow->setStatusBar(statusBar);

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", nullptr));
        label->setText(QApplication::translate("MainWindow", "TextLabel", nullptr));
        label_2->setText(QApplication::translate("MainWindow", "TextLabel", nullptr));
    } // retranslateUi

};

ui->setupUi(this)是由.ui文件生成的类的构造函数,这个函数的作用是对界面进行初始化,它按照我们在Qt设计器里设计的样子把窗体画出来,把我们在Qt设计器里面定义的信号和槽建立起来。

  • 写代码的一个BUG

今天在使用Qt写代码的时候发现总提示内存泄漏,但是我的代码非常简单,如下:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    qDebug() <<pixmap.load("resources/map.png");

    if(!pixmap.isNull())
    {
        qDebug() << "显示雷达图像";
        ui->label->setPixmap(pixmap);
    }

    ui->label_2->setText("hello word");

    ui->setupUi(this);
}

经过排查之后发现,我把ui->setupUi(this)这行代码写在了最后,Qt在ui->setupUi(this)中对label进行了内存的分配

        label_2 = new QLabel(centralWidget);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setGeometry(QRect(60, 20, 141, 31));

只有分配了内存,才能使用label,所以说一定要把 ui->setupUi(this)这行代码放在函数一开始的位置。

  • 解决BUG

将代码修改如下后,不在提示内存泄漏问题:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    qDebug() <<pixmap.load("resources/map.png");

    if(!pixmap.isNull())
    {
        qDebug() << "显示雷达图像";
        ui->label->setPixmap(pixmap);
    }

    ui->label_2->setText("hello word");
}
原文地址:https://www.cnblogs.com/Manual-Linux/p/11490614.html