当前位置: 首页> 教育> 高考 > 徐州网站制作公司哪家好_深圳平面设计公司招聘_有哪些搜索引擎_东莞seo建站公司

徐州网站制作公司哪家好_深圳平面设计公司招聘_有哪些搜索引擎_东莞seo建站公司

时间:2025/7/16 6:18:40来源:https://blog.csdn.net/qq_62435866/article/details/146976091 浏览次数:1次
徐州网站制作公司哪家好_深圳平面设计公司招聘_有哪些搜索引擎_东莞seo建站公司

Qt实现登录界面

  • 1、设计界面
  • 2、创建密码对象并初始化
  • 3、密码定时器实现
    • 3.1添加头文件
    • 3.1创建密码定时器
    • 3.2创建定时器对象并设置为单次触发
    • 3.3创建定时器结束处理槽函数
    • 3.4连接定时器结束信号与槽
    • 3.5定时器结束处理槽函数实现
      • 3.5.1定义一个密码状态变量并初始化
      • 3.5.2槽函数实现
  • 4、创建一个显示更新函数
    • 4.1创建函数
    • 4.2显示更新函数实现
  • 5、事件过滤器实现
    • 5.1重写事件过滤器
    • 5.2安装事件过滤器
    • 5.3事件过滤器实现
  • 6、眼睛按钮实现
    • 6.1添加资源文件
    • 6.2连接信号与槽
    • 6.3按钮实现
  • 7、头文件和.cpp文件
    • 7.1widget.h文件
    • 7.2.cpp文件
  • 8、视频演示
  • 9、总结

1、设计界面

在这里插入图片描述

2、创建密码对象并初始化

QString password;                   //密码

在这里插入图片描述

3、密码定时器实现

3.1添加头文件

在这里插入图片描述

3.1创建密码定时器

QTimer *passWorldTimer;             //密码定时器

在这里插入图片描述

3.2创建定时器对象并设置为单次触发

passwordTimer = new QTimer(this);               //创建密码定时器对象
passwordTimer->setSingleShot(true);             //设置定时器为单次触发

3.3创建定时器结束处理槽函数

void handlePassworrdTimer();                    //密码定时器结束后处理槽函数

3.4连接定时器结束信号与槽

//密码定时器结束处理
connect(passwordTimer,&QTimer::timeout,this,&Widget::handlePassworrdTimer); 

3.5定时器结束处理槽函数实现

3.5.1定义一个密码状态变量并初始化

bool passwordstate;                 //密码状态
passwordState = false;        //初始化显示为密文 

3.5.2槽函数实现

//定时器结束处理槽函数
void Widget::handlePassworrdTimer()
{if(!passwordState && !password.isEmpty()){QString displayText = QString(password.length(),QChar(0X25CF));         //变为密文ui->passwordLineEdit->setText(displayText);          //设置密码框文本}
}

4、创建一个显示更新函数

4.1创建函数

在这里插入图片描述

4.2显示更新函数实现

//显示更新
void Widget::updatePasswordDisplay()
{//明文状态if(passwordState){ui->passwordLineEdit->setText(password);         //显示明文}else{//密码为空if(password.isEmpty()){ui->passwordLineEdit->setText("");           //设置为空}else        //密码不为空{//除了最后一个密码,全部变成小黑点QString displayText = QString(password.length()-1,QChar(0x25CF))+password.right(1);ui->passwordLineEdit->setText(displayText);          //显示文本}}
}

5、事件过滤器实现

5.1重写事件过滤器

bool eventFilter(QObject *watched,QEvent *event)override;       //重写事件过滤器

5.2安装事件过滤器

ui->passwordLineEdit->installEventFilter(this);             //密码输入框安装事件过滤器

5.3事件过滤器实现

//事件过滤器
bool Widget::eventFilter(QObject *watched, QEvent *event)
{//判断是否是密码输入框事件并且事件是键盘按下事件if(watched == ui->passwordLineEdit && event->type() == QEvent::KeyPress){QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);       //将通用事件转换为键盘事件//判断撤销键是否按下if(keyEvent->key() == Qt::Key_Backspace){//判断密码是否为空if(password.isEmpty()){QMessageBox::warning(this,"错误","密码为空");      //密码为空提示}else{password.chop(1);           //删掉最后一个字符}}//按下的不是退格键且文本不为空else{//文本不为空if(!keyEvent->text().isEmpty()){password += keyEvent->text();       //在密码后面追加}}updatePasswordDisplay();            //显示更新passwordTimer->stop();              //停止之前的定时器passwordTimer->start(1000);         //密码定时器启动return true;                //事件已经被实现  }return QWidget::eventFilter(watched,event);             //其他事件交给父类处理}

6、眼睛按钮实现

6.1添加资源文件

因为我已经添加过资源文件了,所以我就直接添加现有文件了,否则需要先添加资源文件

在这里插入图片描述

6.2连接信号与槽

在这里插入图片描述

6.3按钮实现

void Widget::on_eyePushButton_clicked()
{passwordstate = !passwordstate;         //密码状态取反//更新图标if(passwordstate){ui->eyePushButton->setIcon(QIcon(":/res/widget/closeeye.png"));    //更改闭眼睛图标}else{ui->eyePushButton->setIcon(QIcon(":/res/widget/eye.png"));         //更改眼睛图标}updatepasswordDisplay();        //刷新显示
}

7、头文件和.cpp文件

7.1widget.h文件

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QMouseEvent>
#include <QDebug>
#include <QMenu>
#include <QPropertyAnimation>
#include <QLineEdit>
#include <QKeyEvent>
#include <QTimer>#include "clientregister.h"QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();protected:void mousePressEvent(QMouseEvent *event) override;                       //重写鼠标点击事件bool eventFilter(QObject *watched,QEvent *event)override;       //重写事件过滤器private slots:void on_registerPushButton_clicked();           //注册按钮void on_loginPushButton_clicked();              //登录按钮void on_coverPushbutton_clicked();              //封面按钮void quitMenuTriggered();                       //菜单被点击后处理槽函数void handlePassworrdTimer();                    //密码定时器结束后处理槽函数void on_eyePushButton_clicked();                //眼睛按钮槽函数private:Ui::Widget *ui;void updatePasswordDisplay();                   //显示更新QSqlDatabase db;            //创建数据库对象QMenu *quitMenu;            //创建一个菜单指针bool loginFrameState;           //侧边框状态QLineEdit passwordLineEdit;         //密码输入框QString password;                   //密码QTimer *passwordTimer;              //密码定时器bool passwordState;                 //密码状态
};
#endif // WIDGET_H

7.2.cpp文件

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget),loginFrameState(false),password("")
{ui->setupUi(this);this->setWindowFlag(Qt::FramelessWindowHint);        //无边框this->setAttribute(Qt::WA_TranslucentBackground);    //半透明QRect coverButtonRect = ui->coverPushbutton->geometry();         //获取封面按钮x,y,宽和高int coverButtonRect_x = coverButtonRect.x();                         //获取x坐标int coverButtonRect_y = coverButtonRect.y();                         //获取y坐标int coverButtonRect_w = coverButtonRect.width();                     //封面按钮宽int coverButtonRect_h = coverButtonRect.height();                   //封面按钮高int hidloginFram_x = coverButtonRect.x() + coverButtonRect.width()-ui->loginFrame->width();   //侧边框隐藏时的x坐标//设置侧边框初始位置ui->loginFrame->setGeometry(hidloginFram_x,coverButtonRect.y()+40,ui->loginFrame->width(),ui->loginFrame->height());//固定封面按钮坐标ui->coverPushbutton->setGeometry(120,120,ui->coverPushbutton->width(),ui->coverPushbutton->height());//固定welcome标签位置ui->welcomeLabel->setGeometry((coverButtonRect_x + coverButtonRect_w)/2-10,coverButtonRect_y,ui->welcomeLabel->width(),ui->welcomeLabel->height());//固定熊猫标签位置ui->pandaLabel->setGeometry((coverButtonRect_x + coverButtonRect_w)/2,coverButtonRect_y+coverButtonRect_h-115,ui->pandaLabel->width(),ui->pandaLabel->height());ui->coverPushbutton->raise();               //确保封面按钮在顶层ui->welcomeLabel->raise();                  //确保welcome标签在顶层ui->pandaLabel->raise();                    //确保熊猫标签在顶层ui->loginFrame->hide();quitMenu = new QMenu(this);             //创建菜单对象QAction *quitAct = new QAction(QIcon(":/widget/quit.png"),"退出",this);quitMenu->addAction(quitAct);        //添加动作//连接菜单退出的信号与槽connect(quitMenu,&QMenu::triggered,this,&Widget::quitMenuTriggered);ui->passwordLineEdit->installEventFilter(this);             //密码输入框安装事件过滤器passwordState = false;        //初始化显示为密文passwordTimer = new QTimer(this);               //创建密码定时器对象passwordTimer->setSingleShot(true);             //设置定时器为单次触发connect(passwordTimer,&QTimer::timeout,this,&Widget::handlePassworrdTimer);        //密码定时器结束处理db = QSqlDatabase::addDatabase("QMYSQL");           //连接数据库驱动db.setHostName("localhost");                        //连接本地主机db.setDatabaseName("registeruser");                 //设置数据库db.setUserName("registeruser");                    //设置用户名db.setPassword("register");                         //设置密码if(db.open()){QMessageBox::information(this,"数据库连接","数据库连接成功");}else{QMessageBox::warning(this,"数据库连接",db.lastError().text());qDebug() << db.lastError().text();}
}Widget::~Widget()
{delete ui;
}//鼠标点击事件
void Widget::mousePressEvent(QMouseEvent *event)
{//如果右键被按下if(event->button() == Qt::RightButton){quitMenu->exec(QCursor::pos());       //在当前鼠标位置显示菜单}}//事件过滤器
bool Widget::eventFilter(QObject *watched, QEvent *event)
{//判断是否是密码输入框事件并且事件是键盘按下事件if(watched == ui->passwordLineEdit && event->type() == QEvent::KeyPress){QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);       //将通用事件转换为键盘事件//判断撤销键是否按下if(keyEvent->key() == Qt::Key_Backspace){//判断密码是否为空if(password.isEmpty()){QMessageBox::warning(this,"错误","密码为空");      //密码为空提示}else{password.chop(1);           //删掉最后一个字符}}//按下的不是退格键且文本不为空else{//文本不为空if(!keyEvent->text().isEmpty()){password += keyEvent->text();       //在密码后面追加}}updatePasswordDisplay();            //显示更新passwordTimer->stop();              //停止之前的定时器passwordTimer->start(1000);         //密码定时器启动return true;                //事件已经被实现}return QWidget::eventFilter(watched,event);             //其他事件交给父类处理}//注册按钮
void Widget::on_registerPushButton_clicked()
{this->hide();           //隐藏登录界面clientregister registerDialog;      //创建新窗口对象registerDialog.setGeometry(this->geometry());       //设置新窗口位置if(registerDialog.exec() == QDialog::Accepted){QMessageBox::information(this,"注册提示","注册成功");}else{this->show();       //显示登录界面}
}//登录按钮
void Widget::on_loginPushButton_clicked()
{QString account = ui->accountLineEdit->text();          //获取账号输入文本
//    QString password = ui->passwordLineEdit->text();          //获取密码输入文本QString inputPassword = password;            //使用实际存储的变量if(account.isEmpty() || inputPassword.isEmpty()){QMessageBox::warning(this,"警告","账号或密码不能为空");return;}//密码加密处理//生成64字符的SHA256哈希值QString hashPassword = QCryptographicHash::hash(inputPassword.toUtf8(),QCryptographicHash::Sha256).toHex();QSqlQuery clientquerry;             //创建查询数据库对象clientquerry.prepare("select * from registerUsers where account=:account and password=:password");      //查询账号和密码clientquerry.bindValue(":account",account);                 //将实际账号绑定到参数clientquerry.bindValue(":password",hashPassword);               //将实际密码绑定到参数if(clientquerry.exec() && clientquerry.next()){QMessageBox::information(this,"成功","登录成功");          //提升登录成功}else{QMessageBox::warning(this,"失败","账号或密码错误");          //提升登录成功}}//封面按钮
void Widget::on_coverPushbutton_clicked()
{QRect coverButtonRect = ui->coverPushbutton->geometry();           //获取封面按钮x,y宽和高QRect loginFrameRect = ui->loginFrame->geometry();                 //获取登录界面按钮x,y宽和高int targetX;        //目标位置参数//侧边框展开if(loginFrameState){targetX = coverButtonRect.x()+coverButtonRect.width()-loginFrameRect.width();          //侧边框展开时目标位置}else{targetX = coverButtonRect.x()+coverButtonRect.width()-20;           //侧边框隐藏时位置}QPropertyAnimation *animation = new QPropertyAnimation(ui->loginFrame,"geometry");         //为侧边框创建动画对象animation->setDuration(500);            //动画持续时间animation->setEasingCurve(QEasingCurve::OutQuad);       //设置动画缓动曲线。开始变慢,让然后加快//设置动画范围ui->loginFrame->hide();animation->setStartValue(loginFrameRect);               //动画开始位置animation->setEndValue(QRect(targetX-5, coverButtonRect.y()+40,loginFrameRect.width(),loginFrameRect.height()));   //动画结束位置ui->loginFrame->show();//状态切换connect(animation,&QPropertyAnimation::finished,[this](){loginFrameState = !loginFrameState;         //侧边框状态翻转});animation->start(QAbstractAnimation::DeleteWhenStopped);        //开始,结束自动删除动画
}//菜单被点击后处理槽函数
void Widget::quitMenuTriggered()
{QMessageBox quitMes;         //创建一个退出询问信息框quitMes.setWindowTitle("关闭窗口");           //设置窗口标题quitMes.setIcon(QMessageBox::Warning);      //设置一个警告图片quitMes.setText("是否退出");                 //设置窗口文本quitMes.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);      //设置两个按钮quitMes.setWindowIcon(QIcon(":/widget/pandaQuit.png"));                 //设置窗口图标quitMes.setButtonText(QMessageBox::Ok,"确定");            //Ok改为确认quitMes.setButtonText(QMessageBox::Cancel,"取消");        //Cancle改为取消int result = quitMes.exec();            //显示信息框等待用户交互//点击了Okif(result == QMessageBox::Ok){this->close();          //退出界面}//否则啥都不做
}//定时器结束处理槽函数
void Widget::handlePassworrdTimer()
{if(!passwordState && !password.isEmpty()){QString displayText = QString(password.length(),QChar(0X25CF));         //变为密文ui->passwordLineEdit->setText(displayText);          //设置密码框文本}
}//显示更新
void Widget::updatePasswordDisplay()
{//明文状态if(passwordState){ui->passwordLineEdit->setText(password);         //显示明文}else{//密码为空if(password.isEmpty()){ui->passwordLineEdit->setText("");           //设置为空}else        //密码不为空{//除了最后一个密码,全部变成小黑点QString displayText = QString(password.length()-1,QChar(0x25CF))+password.right(1);ui->passwordLineEdit->setText(displayText);          //显示文本}}
}//眼睛按钮
void Widget::on_eyePushButton_clicked()
{passwordState = !passwordState;         //密码状态取反//更新图标//明文状态if(passwordState){ui->eyePushButton->setIcon(QIcon(":/widget/closeeye.png"));             //闭眼图标}else    //密文状态{ui->eyePushButton->setIcon(QIcon(":/widget/eye.png"));                  //睁眼图标}updatePasswordDisplay();            //更新显示
}

8、视频演示

密码小黑点

9、总结

以上就是Qt实现登录界面的整个过程了,浏览过程中,如若发现错误,欢迎大家指正,

有问题的可以评论区留言或者私信。头文件和.cpp文件代码里面还有一些别的功能,大

家可以选择性参考和借鉴,想要源码的可以评论区留言或者私信,最后,如果大家觉得

有所帮助的话,可以点个赞,谢谢大家!梦虽遥,追则能达;愿虽艰,持则可圆!
关键字:徐州网站制作公司哪家好_深圳平面设计公司招聘_有哪些搜索引擎_东莞seo建站公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: