前言
本文主要参考资料:
- Jasmin Blanchette<C++ GUI QT4编程(第二版)> 电子工业出版社
本文主要实现功能为:编写一个对话框。其运行效果如下图所示:
本文使用软件对应版本为:
- QT creator4.4.1
- QT4.8.7
- MinGW 4.8.2
新建项目
- 快捷键:Ctrl+Shift+N,或者选择文件-新建项目或文件。出现下图
- 如上图,选择Application-> QT widgets Application。然后点击choose,得到如下界面。
- 如上图,在名称一栏填入项目名称,如Demo。在创建路径中填入项目地址。点击下一步按钮。
- 继续点击下一步。因为我们创建的是对话框,所以在基类选项中选择QDialog。类名自己根据实际定义,这里填写为HelloDialog。其余文件名称会自动修改。
- 点击下一步。
- 此页面默认,选择完成。
可视化布局控件
首先,双击hellodialog.ui进行可视化布局。
控件选择
选择以下三种控件,并大致进行排列。
- Dialog Button Box
- Line Edit
- Label
修改控件属性
- 单击TextLabel,并修改text属性为“名称(&N)“。
- 单击窗体,修改窗体的标题为:“你好!”。
设置伙伴关系与TAB顺序
因为此对话框TAB选项只有一个,所以,不再演示。其设置方式在上图已标注。单击“Edit Buddies”。点击名称(&N)标签,且拖住到lineEdit控件,在放鼠标。此时,会通过蓝色箭头将两者连接起来。表明伙伴关系建立成功。
此时,可以看到“&”字符消失不见。&标记是用来标记快捷键。此时表明当程序运行时,键盘输入ALT+N,lineEidt就会被选中。
最后,按F3或者单击“编辑窗口”退出编辑伙伴关系。
窗体布局
- 同时选择label和lineEdit,且单击水平布局。
- 同理,将单击对话框HelloDialog。并单击竖直布局。
- 最后自动调整对话框大小。单击HelloDialog,且单击调整大小。
至此,可视化布局部分完成,Ctrl+S保存数据。
编写程序
在新建项目的时候,Qt Creator 已经创建了对话框的头文件和c文件,以及显示对话框的main函数。我们编写程序只要基于以上修改即可。注释不祥部分,可以参考上篇博客<QT第一篇——创建对话框>或者参考本文的参考文献。
hellodialog.h
#ifndef HELLODIALOG_H
#define HELLODIALOG_H
#include <QDialog>
namespace Ui {
class HelloDialog;
}//当程序编译的时候,会自动根据ui文件生成布局相关的类Ui_HelloDialog。此处声明的类,及Ui::HelloDialog为其空子类。
class HelloDialog : public QDialog
{
Q_OBJECT
public:
explicit HelloDialog(QWidget *parent = 0);//构造函数
~HelloDialog();//析构函数
private slots:
void on_lineEdit_textChanged();//私有槽函数,当lineEdit控件发射textChanged信号时,自动调用此槽函数。
private:
Ui::HelloDialog *ui;//声明一个私有ui布局相关对象
};
#endif // HELLODIALOG_H
hellodialog.cpp
#include "hellodialog.h"
#include "ui_hellodialog.h"
#include<QtGui>
HelloDialog::HelloDialog(QWidget *parent) :
QDialog(parent),//调用父类构造函数
ui(new Ui::HelloDialog)//创建一个ui对象
{
//初始化窗体
ui->setupUi(this);//初始化窗体,载入窗体布局,自动调用命名符合规则的槽函数。
//ButtonBox中OK键不使能。
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
//输入名称符合正则表达式规则
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");//第一个字符为字母,第二个字符为1-9的数字,后面接0-2个数字字符
ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));//将QRegExpValidator对象设置为this的一个子对象,当this对象删除,该对象自送删除。
//设置信号槽连接
connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(accept()));
connect(ui->buttonBox,SIGNAL(rejected()),this,SLOT(reject()));
}
HelloDialog::~HelloDialog()
{
delete ui;
}
void HelloDialog::on_lineEdit_textChanged()
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ui->lineEdit->hasAcceptableInput());//当lineEdit对象输入符合正则表达式,ok键使能。
}
mian.c
#include "hellodialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HelloDialog w;
w.show();
return a.exec();
}
ui_hellodialog.h
此文件不需要编写,运行时根据ui文件自动生成。
/********************************************************************************
** Form generated from reading UI file 'hellodialog.ui'
**
** Created by: Qt User Interface Compiler version 4.8.7
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_HELLODIALOG_H
#define UI_HELLODIALOG_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QVBoxLayout>
QT_BEGIN_NAMESPACE
class Ui_HelloDialog
{
public:
//窗口上使用到的控件声明
QVBoxLayout *verticalLayout;
QHBoxLayout *horizontalLayout;
QLabel *label;
QLineEdit *lineEdit;
QDialogButtonBox *buttonBox;
//将对话框对象传入到该函数中,进行布局
void setupUi(QDialog *HelloDialog)
{
if (HelloDialog->objectName().isEmpty())
HelloDialog->setObjectName(QString::fromUtf8("HelloDialog"));//设定对话框默认名称
HelloDialog->resize(210, 69);
//竖直布局设定
verticalLayout = new QVBoxLayout(HelloDialog);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
//水平布局设定
horizontalLayout = new QHBoxLayout();
horizontalLayout->setSpacing(6);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
//标签设定
label = new QLabel(HelloDialog);
label->setObjectName(QString::fromUtf8("label"));
horizontalLayout->addWidget(label);
//行编辑器设定
lineEdit = new QLineEdit(HelloDialog);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
horizontalLayout->addWidget(lineEdit);
verticalLayout->addLayout(horizontalLayout);
//按键盒子设定
buttonBox = new QDialogButtonBox(HelloDialog);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
verticalLayout->addWidget(buttonBox);
#ifndef QT_NO_SHORTCUT
label->setBuddy(lineEdit);
#endif // QT_NO_SHORTCUT
retranslateUi(HelloDialog);//设定空间显示文字
QMetaObject::connectSlotsByName(HelloDialog);//根据定义名称,自动调用槽函数
} // setupUi
void retranslateUi(QDialog *HelloDialog)
{
HelloDialog->setWindowTitle(QApplication::translate("HelloDialog", "\344\275\240\345\245\275\357\274\201", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("HelloDialog", "\345\220\215\347\247\260(&N)\357\274\232", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class HelloDialog: public Ui_HelloDialog {};//定义一个空子类Ui::HelloDialog
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_HELLODIALOG_H