Qt5:快速设计对话框 QtDesigner 的使用

时间:2022-04-02 23:14:09

QtDesigner  的使用 比较简单  ,  一般摸索几下就可以使用大部分的功能了

 

这里主要介绍其常用的功能

1、对用户界面进行可视化的搭建 和 布局

2、设置 tab 键的顺序 

3、设置信号-槽的关系

4、自定义槽

5、设置窗口部件的各种属性

 

QtDesigner的主要使用方法就是

首先  创建一个  QtDesigner 文件,  就是一个  后缀 为 *.ui   的文件

这种ui文件是基于xml格式的一种文件

点击这个文件,就自动使用  可视化编辑器打开了

当用户界面建立完成后  , 保存

使用  qmake  编译这个文件 , 就会得到一个  以   ui_*.h  的头文件 ,里面包含用户界面的实现代码

打开  *.pro   工程文件  ,  在 HEADERS +=  这一項中加上  ui_*.h  头文件 ,  然后保存  ,  然后就会发现  ui_*.h  头文件被自动包含到工程里面了

当要使用搭建好的界面的时候   就要经历以下步骤

  包含  ui_*.h  头文件

  创建一个  window  , 例如  QWidget * window = new QWinget;

  然后创建一个   Ui::XXXX  *   ui = new Ui::XXXX;  (  该类是在ui_*.h  中定义的 )

  然后调用如下语句  ui->setupUi(window) ;  该语句 是将 定义好的 UI 界面应用到刚刚创建的 window 上

    然后就可以show()  了

实例代码

  

 1 /********************main.cpp*******************************/
2 #include"gotocelldialog.h"
3 #include <QApplication>
4 #include<QDialog>
5
6 int main(int argc, char *argv[])
7 {
8 QApplication a(argc, argv);
9 GotoCellDialog* dlg = new GotoCellDialog;
10 dlg->show();
11 return a.exec();
12 }
 1 /*****************************gotocelldialog.h**********************************/
2 #ifndef GOTOCELLDIALOG_H
3 #define GOTOCELLDIALOG_H
4 #include<QDialogButtonBox>
5 #include<QString>
6 #include"ui_dialog.h"
7
8 class GotoCellDialog:public QDialog, public Ui::Dialog
9 {
10 Q_OBJECT
11 public:
12 GotoCellDialog(QWidget* parent = 0);
13 ~GotoCellDialog();
14 private slots:
15 void on_lineEdit_textChanged(const QString & txt);
16 void on_okBtn_clicked();
17 void on_cancelBtn_clicked();
18 };
19
20 #endif // GOTOCELLDIALOG_H
 1 /**********************gotocelldialog.cpp*********************************/
2 #include<QtGui>
3 #include<QLabel>
4 #include "gotocelldialog.h"
5
6 GotoCellDialog::GotoCellDialog(QWidget* parent)
7 : QDialog(parent)
8 {
9 this->setupUi(this);
10 QRegExp reg("[a-zA-Z][0-9][0-9]{0,2}");
11 lineEdit->setValidator(new QRegExpValidator(reg,this));
12 }
13
14 void GotoCellDialog::on_lineEdit_textChanged(const QString & txt)
15 {}
16
17 void GotoCellDialog::on_okBtn_clicked()
18 {
19 this->setWindowTitle(lineEdit->text());
20 okBtn->update();
21 }
22 void GotoCellDialog::on_cancelBtn_clicked()
23 {
24 this->close();
25 }
26
27 GotoCellDialog::~GotoCellDialog()
28 {}
 1 /***************************gotocelldialog.pro*******************************/
2 //这是添加了 ui_*.h 的工程文件
3
4 #-------------------------------------------------
5 #
6 # Project created by QtCreator 2013-05-31T12:59:16
7 #
8 #-------------------------------------------------
9
10 QT += core gui
11
12 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
13
14 TARGET = QtDesigner_Test
15 TEMPLATE = app
16
17
18 SOURCES += main.cpp\
19 mainwindow.cpp\
20 gotocelldialog.cpp
21
22 HEADERS += mainwindow.h\
23 ui_dialog.h\
24 gotocelldialog.h
25
26 FORMS += mainwindow.ui \
27 dialog.ui