其他分享
首页 > 其他分享> > QT登陆界面,运用正则表达式,时间显示和定时器

QT登陆界面,运用正则表达式,时间显示和定时器

作者:互联网

<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