标签:定时器 QT mainLayout 正则表达式 tr Dialog addWidget new include
<dialog.h>
#ifndef DIALOG_H
#define DIALOG_H
#include<Qlabel>
#include<QLineEdit>
#include<QPushButton>
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
protected:
QLabel *label1,*label2,*datalabel;
QLineEdit *lineEdit1,*lineEdit2;
QPushButton *button1,*button2,*button3;
public slots:
void showTime();
void clear();
private:
QTimer *m_timer;
void InitTimer();
};
#endif // DIALOG_H
<dialog.cpp>
#include "dialog.h"
#include <QDebug>
#include<QGridLayout>
#include<QDateTime>
#include<QTimer>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
label1=new QLabel(this);
label1->setText(tr("用户名:"));
label2=new QLabel(this);
label2->setText(tr("密码:"));
datalabel=new QLabel(this);
lineEdit1=new QLineEdit(this);
lineEdit1->setPlaceholderText("4位字母用户名");
lineEdit2=new QLineEdit(this);
lineEdit2->setPlaceholderText("5位数字密码");
button1=new QPushButton(this);
button1->setText(tr("登陆"));
button2=new QPushButton(this);
button2->setText(tr("退出"));
button3=new QPushButton(this);
button3->setText(tr("重置"));
QGridLayout*mainLayout=new QGridLayout(this);//用于布局管理,将所有控件的位置固定
mainLayout->addWidget(label1,0,0);
mainLayout->addWidget(lineEdit1,0,1);
mainLayout->addWidget(label2,1,0);
mainLayout->addWidget(lineEdit2,1,1);
mainLayout->addWidget(button1,2,0);
mainLayout->addWidget(button2,2,1);
mainLayout->addWidget(button3,2,2);
mainLayout->addWidget(datalabel,3,0);
connect(button1,SIGNAL(clicked()),this,SLOT(showTime()));
connect(button2,&QPushButton::clicked,this,&Dialog::close);
connect(button3,SIGNAL(clicked()),this,SLOT(clear()));
}
Dialog::~Dialog()
{
}
void Dialog::showTime()
{
if(QRegExp(tr("[a-z][a-z][a-z][a-z]")).exactMatch(lineEdit1->text())&&QRegExp(tr("0|[1-9]\\d{0,4}")).exactMatch(lineEdit2->text()))
{
QTimer *time=new QTimer(this);
connect(time,SIGNAL(timeout()),this,SLOT(showTime()));
time->start(2000);
QDateTime *datatime=new QDateTime(QDateTime::currentDateTime());
QString str=datatime->toString("yyyy-MM-dd hh:mm:ss dddd");
datalabel->setText(str);
}
}
void Dialog::clear()
{
lineEdit1->clear();
lineEdit2->clear();
datalabel->clear();
}
正则表达式:QRegExp(tr("*****")).exactMatch(lineEdit->text())
使用QRegExp::exactMatch来判断一个字符串是否符合
显示实时时间:QDateTime *datatime=new QDateTime(QDateTime::currentDateTime());
QString str=datatime->toString(“yyyy-MM-dd hh:mm:ss dddd”);
datalabel->setText(str);
定时器:QTimer *time=new QTimer(this);
connect(time,SIGNAL(timeout()),this,SLOT(showTime()));
time->start(2000);
标签:定时器,QT,mainLayout,正则表达式,tr,Dialog,addWidget,new,include 来源: https://blog.csdn.net/talouo/article/details/87989633
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。