Qt 登录界面设计
来源:互联网 发布:ida mac 64位 破解版 编辑:程序博客网 时间:2024/05/01 16:18
程序简介:
创建一个用户注册界面,用户输入注册信息后,点击“注册”按钮,将用户信息写入文件,并弹出对话框显示“xxx用户,您好!恭喜您已经注册成功。”
写入文件的用户信息
用户名、姓名、邮箱、性别、出生日期、注册日期时间、个人爱好等
要求:注册用户名必须唯一,如有重复则注册不成功,程序应给出提示;邮箱检查合法性;性别要求选择;出生日期从下拉菜单或日历中选择;注册日期时间为系统时间;个人爱好从列表中多选
在此基础上,我实现了运行程序时弹出一个登陆框, 点击注册弹出一个注册框, 点击登陆显示当前用户信息的功能,使用了背景透明和无边框的设计,使用了Unix风格的配置文件格式,采用了Md5加密密码,用了国际正则表达式限制邮箱格式,并且对所有输入框都进行了一定的限制,保证了程序的稳定
效果图:
使用代码前注意:配置文件应该放在Debug文件下,名字为pw.txt,当然可以自己更改路径,重新命名
代码:
main.cpp:
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); mainwindow w; w.show(); return a.exec();}mainwindow.h:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include "dialog.h"class mainwindow : public QDialog{ Q_OBJECTpublic: explicit mainwindow(QWidget *parent = 0);protected: void paintEvent(QPaintEvent *event);private: QLabel label1, label2; QLineEdit lineedit1, lineedit2; QPushButton button1, button2, button3; QByteArray m_passwdhash; QString m_usrname, m_passwd;private slots: void on_Register_clicked(); void on_Login_clicked(); void on_Exit_clicked();};#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"mainwindow::mainwindow(QWidget *parent) : QDialog(parent) { //设置窗口样式 setAttribute(Qt::WA_TranslucentBackground, true); setWindowFlags(Qt::FramelessWindowHint); setGeometry(QRect(750, 300, 400, 400)); //设置密码显示样式 lineedit2.setEchoMode(QLineEdit::Password); label1.setText(tr("Username")); label2.setText("Passwd "); button1.setText("regist"); button2.setText("login"); button3.setText("exit"); QPalette palette; palette.setColor(QPalette::WindowText, Qt::blue); label1.setPalette(palette); label2.setPalette(palette); QHBoxLayout *hboxlayout1 = new QHBoxLayout; QHBoxLayout *hboxlayout2 = new QHBoxLayout; QHBoxLayout *hboxlayout3 = new QHBoxLayout; hboxlayout1->addWidget(&label1); hboxlayout1->addWidget(&lineedit1); hboxlayout2->addWidget(&label2); hboxlayout2->addWidget(&lineedit2); hboxlayout3->addWidget(&button1); hboxlayout3->addWidget(&button2); hboxlayout3->addWidget(&button3); QVBoxLayout *vboxlayout = new QVBoxLayout; vboxlayout->addLayout(hboxlayout1); vboxlayout->addLayout(hboxlayout2); vboxlayout->addLayout(hboxlayout3); setLayout(vboxlayout); connect(&button1, SIGNAL(clicked()), this, SLOT(on_Register_clicked())); connect(&button2, SIGNAL(clicked()), this, SLOT(on_Login_clicked())); connect(&button3, SIGNAL(clicked()), this, SLOT(on_Exit_clicked()));}void mainwindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.fillRect(this->rect(), QColor(0, 0, 255, 80));}void mainwindow::on_Register_clicked(){ Dialog w; w.show(); w.exec();}void mainwindow::on_Login_clicked(){ //检验用户名或密码是否为空 if (lineedit1.text().isEmpty() || lineedit2.text().isEmpty()) { QMessageBox::warning(this, tr("warning"), tr("username or password is empty, you can`t login!")); return; } m_usrname = lineedit1.text(); m_passwdhash = QCryptographicHash::hash(lineedit2.text().toUtf8(), QCryptographicHash::Md5); m_passwd = m_passwdhash.toHex(); QFile file("pw.txt"); QTextStream filestream(&file); //读取文件进行校准 if (!file.open(QIODevice::ReadOnly)) { QMessageBox::warning(this, tr("warning"), tr("open file fail!") + file.errorString()); return; } while (!filestream.atEnd()) { QByteArray temp; filestream >> temp; temp.trimmed(); if (temp.isEmpty()) { filestream.skipWhiteSpace(); continue; } if (temp.startsWith('#')) { filestream.readLine(); continue; } QList<QByteArray> list = temp.split('='); if (list.count() < 2) { QMessageBox::warning(this, tr("warning"), tr("user file error!")); return; } QString str1 = QString::fromLocal8Bit(list[0].trimmed()); QString str2 = QString::fromLocal8Bit(list[1].trimmed()); if (str1 == "username" && str2 == m_usrname) { QString strMsg; strMsg += (str1 + ": " + str2 + "\n"); while (!filestream.atEnd()) { QByteArray t; filestream >> t; t.trimmed(); if (t.isEmpty() || t.startsWith('#')) break; QList<QByteArray> l = t.split('='); if (l.count() < 2) { QMessageBox::warning(this, tr("warning"), tr("user file error!")); return; } QString s1 = QString::fromLocal8Bit(l[0].trimmed()); QString s2 = QString::fromLocal8Bit(l[1].trimmed()); if (s1 == "password" && s2 != m_passwd) { QMessageBox::warning(this, tr("user message"), tr("password false!")); return; } strMsg += (s1 + ": " + s2 + "\n"); } QMessageBox::information(this, tr("user message"), strMsg); return; } } QMessageBox::warning(this, tr("warning"), tr("username error")); file.close();}void mainwindow::on_Exit_clicked(){ this->close();}dialog.h:
#ifndef DIALOG_H#define DIALOG_H#include <QtWidgets>#include <QDialog>namespace Ui {class Dialog;}class Dialog : public QDialog{ Q_OBJECTpublic: explicit Dialog(QWidget *parent = 0); ~Dialog();private: Ui::Dialog *ui; QByteArray m_passwdhash; QString m_usrname, m_passwd;protected: void paintEvent(QPaintEvent *event);private slots: void on_Register_clicked(); void on_Exit_clicked();};#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); //设置窗口样式 setAttribute(Qt::WA_TranslucentBackground, true); setWindowFlags(Qt::FramelessWindowHint); setGeometry(QRect(650, 200, 500, 500)); //设置密码显示样式 ui->Passwd->setEchoMode(QLineEdit::PasswordEchoOnEdit); ui->Passwd_r->setEchoMode(QLineEdit::Password); //设置邮箱正则表达式 QRegExp re("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); QRegExpValidator *reVali = new QRegExpValidator(re); ui->Email->setValidator(reVali); //设置lineedit的最大输入长度 ui->Usrname->setMaxLength(10); ui->Passwd->setMaxLength(16); ui->Passwd_r->setMaxLength(16); ui->Email->setMaxLength(30); ui->Name->setMaxLength(20); //弹出日历 ui->Age->setCalendarPopup(true);}Dialog::~Dialog(){ delete ui;}void Dialog::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.fillRect(this->rect(), QColor(0, 0, 255, 80));}void Dialog::on_Register_clicked(){ //检查信息是否完善 if (ui->Usrname->text().isEmpty() || ui->Passwd->text().isEmpty() || ui->Passwd_r->text().isEmpty() || ui->Name->text().isEmpty() || (!ui->Boy->isChecked() && !ui->Girl->isChecked()) || (!ui->C->isChecked() && !ui->Java->isChecked() && !ui->Python->isChecked())) { QMessageBox::warning(this, tr("warning"), tr("you must improve your information!")); return; } //检查两次密码是否一致 if (ui->Passwd->text() != ui->Passwd_r->text()) { QMessageBox::warning(this, tr("warning"), tr("two passwds are different!")); return; } m_usrname = ui->Usrname->text(); m_passwdhash = QCryptographicHash::hash(ui->Passwd->text().toUtf8(), QCryptographicHash::Md5); m_passwd = m_passwdhash.toHex(); QString m_email = ui->Email->text(); QString m_name = ui->Name->text().toUtf8(); QString m_sex; if (ui->Boy->isChecked()) m_sex = "boy"; else m_sex = "girl"; QDate data = ui->Age->date(); QString m_age = data.toString("yyyy-MM-dd"); QString m_hobby; if (ui->C->isChecked()) m_hobby += "CorC++"; if (ui->Java->isChecked()) { if (!m_hobby.isEmpty()) m_hobby += "&"; m_hobby += "Java"; } if (ui->Python->isChecked()) { if (!m_hobby.isEmpty()) m_hobby += "&"; m_hobby += "Python"; } QDateTime time = QDateTime::currentDateTime(); QString m_registtime = time.toString("yyyy-MM-dd hh:mm:ss ddd"); QFile file("pw.txt"); QTextStream filestream(&file); QFile filex("pw.txt"); QTextStream filestreamx(&filex); //邮箱检验 int flag = 0; for (int i = 0; i <m_email.size(); i++) { if (m_email[i] == '@' || m_email[i] == '.') flag++; } if (flag < 2 || m_email[m_email.size() - 1] == '.') { QMessageBox::warning(this, tr("warning"), tr("email adress error!")); return; } //检验用户名格式 for (int i = 0; i < m_usrname.size(); i++) { if (m_usrname[i] == ' ' || m_usrname[i] == '\r' || m_usrname[i] == '\n') { QMessageBox::warning(this, tr("warning"), tr("username format error!")); return; } } //检验密码格式 for (int i = 0; i < m_passwd.size(); i++) { if (m_passwd[i] == ' ' || m_passwd[i] == '\r' || m_passwd[i] == '\n') { QMessageBox::warning(this, tr("warning"), tr("password format error!")); return; } } //检验名字格式 for (int i = 0; i < m_name.size(); i++) { if (m_name[i] == ' ' || m_name[i] == '\r' || m_name[i] == '\n') { QMessageBox::warning(this, tr("warning"), tr("name format error!")); return; } } //从用户文档里读取用户信息进行核对 if (!file.open(QIODevice::ReadOnly)) { QMessageBox::warning(this, tr("warning"), tr("open file fail!") + file.errorString()); return; } while (!filestream.atEnd()) { QByteArray temp; filestream >> temp; temp.trimmed(); if (temp.isEmpty()) { filestream.skipWhiteSpace(); continue; } if (temp.startsWith('#')) { filestream.readLine(); continue; } QList<QByteArray> list = temp.split('='); if (list.count() < 2) { QMessageBox::warning(this, tr("warning"), tr("user file error!")); return; } QString str1 = QString::fromLocal8Bit(list[0].trimmed()); QString str2 = QString::fromLocal8Bit(list[1].trimmed()); if (str1 == "username" && str2 == m_usrname) { QMessageBox::warning(this, tr("warning"), tr("username has been existed!")); return; } } file.close(); //写入用户文档 if (!filex.open(QIODevice::WriteOnly | QIODevice::Append)) { QMessageBox::warning(this, tr("warning"), tr("open file fail!") + filex.errorString()); return; } QMessageBox::information(this, tr("user message"), tr("regist success!")); filestreamx << "#" + m_registtime << endl; filestreamx << "username=" + m_usrname << endl; filestreamx << "password=" + m_passwd << endl; filestreamx << "email=" + m_email << endl; filestreamx << "name=" + m_name << endl; filestreamx << "sex=" + m_sex << endl; filestreamx << "age=" + m_age << endl; filestreamx << "hobby=" + m_hobby << endl; file.close(); this->close();}void Dialog::on_Exit_clicked(){ this->close();}
阅读全文
1 0
- Qt 登录界面设计
- 一个较完整的Qt登录界面设计
- 《Qt学习笔记》第一章--登录界面设计
- 登录界面设计
- 登录界面设计
- 登录界面设计
- QT界面设计
- VB登录界面设计代码
- 软件登录界面设计
- 登录界面设计分享
- Android精美登录界面设计
- Android精美登录界面设计
- Android精美登录界面设计
- Android精美登录界面设计
- Android精美登录界面设计
- Android精美登录界面设计
- Android精美登录界面设计
- Android精美登录界面设计
- Quartz任务调度框架学习三(Cron Expressions(计划任务表达式)详解)
- AS——android NDK开发工具使用
- spring 使用注解自动装配和自动扫描机制
- [数据库事务与锁]详解八:底理解数据库事务乐观锁的一种实现方式——CAS
- javascript BOM
- Qt 登录界面设计
- struts中的ognl
- html5解决大文件断点续传
- 接口与多态
- idea mybatis BindingException: Invalid bound statement
- 8 种 NoSQL 数据库系统对比
- 鼠标移动到图片上图片逐渐变大、变清晰(带有过渡效果)
- EditText中inputType和imeOptions属性
- python流量监控脚本