【Qt5开发及实例】2、正则表达式的验证

1、首先还是给一个效果图:

然后就是各个部分的代码,首先这个界面是要自己画的,按钮的名字:

那个Cell Location  的名字是 label

文本框的名字是 : lineEdit

左边的按钮 OK :  okButton

右边的按钮 :  cancelButton

头文件dialog.h

/**
 * <p>我tm终于练成了!Hello World终于打印出来了! %>_<% 我先哭(激动)会~~~<p>
 *
 */
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
  class Dialog;
}

class Dialog : public QDialog
{
  Q_OBJECT

public:
  explicit Dialog(QWidget *parent = 0);
  ~Dialog();

private:
  Ui::Dialog *ui;

private slots:
  void on_lineEdit_textChanged(); //这个是一个函数

};

#endif // DIALOG_H

dialog.cpp定义文件

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog)
{
  ui->setupUi(this);  //初始化界面,刚刚建立的伙伴关系,这个会自动对slot进行匹配
  //就是这个connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(on_lineEdit_textChanged()));

  QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); //正则表达式限制输入字元的范围,记住这里很严格0和2之间只能有个逗号“,”多余的一点都不能
  ui->lineEdit->setValidator(new QRegExpValidator(regExp, this)); //设定正则表达式,这个就是只允许第一个字元输入大小写英文字母,后面接一位非0的数字,再接0~2位可为0的数字

  connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept())); //按下确定按钮做出的反应
  connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject())); //按下取消按钮,就会关闭视窗

}

//实现那个定义的槽
void Dialog::on_lineEdit_textChanged()
{
  ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput());
}

Dialog::~Dialog()
{
  delete ui;
}

主函数main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  w.show();

  return a.exec();
}

好的今天Qt就到这了,哎!期末了时间是越来越紧了,但是越是忙学的东西越是多,努力!!!

时间: 2024-07-29 00:11:17

【Qt5开发及实例】2、正则表达式的验证的相关文章

【Qt5开发及实例】24、数据柱形图显示

数据柱形图显示 1.我们首先把这个这个视图的表格部分表示出来 mainwindow.h /** * 书本:[Qt5开发及实例] * 功能:数据柱形图显示,这个类是表格显示 * 文件:mainwindow.h * 时间:2015年1月28日18:50:54 * 作者:cutter_point */ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QStandardItemModel>

【Qt5开发及实例】25、实现代理的功能

实现代理的功能 在Qt里面也有MVC,那就是视图,模型,代理,后面我们再开一章,好好来学习一下Qt的MVC吧! main.cpp /** * 书本:[Qt5开发及实例] * 功能:实现代理的功能 * 文件:main.cpp * 时间:2015年1月29日20:53:04 * 作者:cutter_point */ #include <QApplication> #include <QStandardItemModel> #include <QTableView> //#i

【Qt5开发及实例】18、图形修饰小工具

图形修饰小工具 无力吐槽,这是我第三遍写这个了,到底是个什么意思???我只要一贴代码,浏览器直接崩溃,呵呵了,我也是,我现在只要写完一段字我就保存,尼玛在掉我就不写了,写到word里面,再贴上来. 效果 左边图形展示界面 paintarea.h /** * 书本:[Qt5开发及实例] * 功能:实现绘画各种图形 * 文件:paintarea.h * 时间:2015年1月21日16:59:25 * 作者:cutter_point */ #ifndef PAINTAREA_H #define PAI

Qt5主窗体程序: 文本编辑器的实现(Qt5开发及实例)

下面的程序出自<Qt5开发及实例>陆文周. 效果图: 程序是一个文本编辑器的实例,主界面如下: 程序中的源文件和头文件: 源代码: 文件main.cpp的源代码: #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QFont font("楷体",15); a.setFont

【Qt5开发及实例】27、获得文件的信息

fileinfo.h /** * 书本:[Qt5开发及实例] * 功能:获得文件的信息 * 文件:fileinfo.h * 时间:2015年2月4日17:23:38 * 作者:cutter_point */ #ifndef FILEINFO_H #define FILEINFO_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QLineEdit> #include

【Qt5开发及实例】28、获取本机网络信息

networkinformation.h /** * 书本:[Qt5开发及实例] * 功能:获取本机网络信息 * 文件:networkinformation.h * 时间:2015年2月5日14:51:33 * 作者:cutter_point */ #ifndef NETWORKINFORMATION_H #define NETWORKINFORMATION_H #include <QWidget> #include <QLabel> #include <QLineEdit&

【Qt5开发及实例】29、实现服务器端的编程,UDP协议

udpserver.h /** * 书本:[Qt5开发及实例] * 功能:实现服务器端的编程 * 文件:udpserver.h * 时间:2015年2月5日21:05:21 * 作者:cutter_point */ #ifndef UDPSERVER_H #define UDPSERVER_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QPushButton> #i

【Qt5开发及实例】30、实现客户端的编程,UDP协议

udpclient.h /** * 书本:[Qt5开发及实例] * 功能:实现客户端的编程 * 文件:udpclient.h * 时间:2015年2月5日22:10:30 * 作者:cutter_point */ #ifndef UDPCLIENT_H #define UDPCLIENT_H #include <QDialog> #include <QVBoxLayout> #include <QTextEdit> #include <QPushButton>

【Qt5开发及实例】10、关于进度条的显示

平常我们下载东西总会有一个显示下载了多少的进度条,我们今天来实现一下. 这里有两种显示方式 可以选择,第一个是用了 QProgressBar控件,第二个是用了QProgressDialog控件 progressdlg.h /** * 书本:[Qt5开发及实例] * 功能:为了实现进度条的显示 * 文件:progressdlg.h * 时间:2015年1月2日15:27:10 * 作者:cutter_point */ #ifndef PROGRESSDLG_H #define PROGRESSDL

【Qt5开发及实例】26、得到文件的大小和目录的路径

说实话,我是为了混个持之以恒的标签的 = =,谁叫我过年都没怎么写呢!!!! /** * 书本:[Qt5开发及实例] * 功能:得到文件的大小和目录的路径 * 文件:main.cpp * 时间:2015年2月3日21:18:16 * 作者:cutter_point */ #include <QCoreApplication> #include <QStringList> #include <QDir> #include <QtDebug> //根据给的路径得