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");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM