在QT專案中新增並使用多個UI佈局介面
阿新 • • 發佈:2019-01-05
在工程的“介面檔案”資料夾上右鍵選擇“新增新檔案”;
選擇“Qt -> Qt Designer Form”新增新的佈局檔案;
新建兩個.ui檔案 dialog1.ui,dialog2.ui
記住要修改一下objectName的內容,不然所有的objectName的內容都為Dialog就不好了
以dialog1為例
改完之後,要先編譯一下,這是會自動生成ui_dialog1.h檔案
新增標頭檔案和原始檔
dialog1.h
#ifndef DIALOG1_H
#define DIALOG1_H
#include <QDialog>
namespace Ui {
class Dialog1;
}
class Dialog1 : public QDialog
{
Q_OBJECT
public:
explicit Dialog1(QWidget *parent = 0);
~Dialog1();
private:
Ui::Dialog1 *ui;
};
#endif // DIALOG1_H
dialog1.cpp
#include "ui_dialog1.h"
#include "dialog1.h"
Dialog1::Dialog1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog1)
{
ui->setupUi(this );
setWindowTitle("dialog1");
}
Dialog1::~Dialog1()
{
delete ui;
}
dialog2同理
然後在主程式中建立物件,並呼叫show方法即可
main.cpp
#include "mainwindow.h"
#include "dialog1.h"
#include "dialog2.h"
#include <QApplication>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Dialog1 d1;
Dialog2 d2;
w.show();
d1.show();
d2.show();
return a.exec();
}
在所有的工作做完之後,點執行,可能會出現衝突導致程式終止的問題,這時點選工具欄中的“構建 ”,然後點選“執行qmake”,之後再點選執行就會出現三個視窗,一個主視窗,兩個對話方塊