实现效果:
点击登录,检验用户密码是否正确,正确则弹出消息框,点击ok转到另一个页面
不正确跳出错误消息框,默认选线为Cancel,点击Yes继续登录
点击Cancel跳出问题消息框,默认选项No,单击yes,退出程序,单击No继续登录
以下是实现的部分代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget> //基类QWidget的头文件
#include<QDebug> //信息调试类,用于输出数据使用
#include<QIcon>
#include<QPushButton>
#include<QLabel>
#include<QLineEdit>
#include<QPixmap>
#include"enter_widget.h"
#include"communication.h"
#include<QMessageBox>
class Widget : public QWidget
{Q_OBJECT //QT的信号与槽元对象public:Widget(QWidget *parent = nullptr);//默认参数构造函数声明~Widget();//虚析构声明
private:QLabel *pic;QLabel *tx;QPushButton* enter;QPushButton* login;QPushButton* question;QLabel* user;QLabel* passwd;QLineEdit* user_l;QLineEdit* passwd_l;enter_widget* en;QTimer * time;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include<QDebug>
Widget::Widget(QWidget *parent): QWidget(parent)
{qDebug()<<this->size(); //获取当前姐买你的尺寸qDebug()<<this->width(); //获取当前组件的宽度qDebug()<<this->height(); //获取当前组件的高度this->setWindowTitle("QQ");//this->resize(500,400);//重新设置界面大小//this->resize(QSize(1000,1000));this->setFixedSize(500,400);//设置固定的界面大小//this->setMaximumSize(600,700);//最大界面大小//this->setMinimumSize(200,300);//最小界面大小this->setWindowIcon(QIcon("://msg/qq.png"));pic = new QLabel(this);pic->resize(this->width(),130);pic->setPixmap(QPixmap("://msg/fm.png"));pic->setScaledContents(true);tx = new QLabel(this);tx->resize(100,100);tx->move(200,60);tx->setPixmap(QPixmap("://msg/2.jpg"));tx->setScaledContents(true);enter = new QPushButton;enter->setParent(this);enter->setText("登录");enter->resize(100,50);enter->move(120,300);enter->setIcon(QIcon("://msg/enter.png"));enter->setStyleSheet("border:0px solid black;border-radius:10;background-color:#FFFFFF");login = new QPushButton;login->setParent(this);login->setText("注册");login->resize(100,50);login->move(280,300);login->setIcon(QIcon("://msg/enter.png"));login->setStyleSheet("border:0px solid black;border-radius:10;background-color:#FFFFFF");// question = new QPushButton;
// question->setParent(this);
// question->setText("问题");
// question->resize(50,50);
// question->move(390,300);
// question->setStyleSheet("border:0px solid black;border-radius:10;background-color:#FFFFFF");user = new QLabel;user->setParent(this);user->setText("用户名:");user->resize(60,30);user->setAlignment(Qt::AlignCenter);//user->setFixedSize(50,20);user->move(enter->x(),enter->y()-120);passwd = new QLabel(this);passwd->setText("密 码:");passwd->resize(60,30);passwd->setAlignment(Qt::AlignCenter);//user->setFixedSize(50,20);passwd->move(enter->x(),enter->y()-60);user_l = new QLineEdit(this);user_l->resize(200,30);user_l->move(user->x()+60,user->y());user_l->setPlaceholderText("请输入用户名");user_l->setStyleSheet("border:0px solid black;border-radius:10;background-color:#b0bfbf");passwd_l= new QLineEdit(this);passwd_l->resize(200,30);passwd_l->move(passwd->x()+60,passwd->y());passwd_l->setPlaceholderText("请输入密码");passwd_l->setEchoMode(QLineEdit::Password);passwd_l->setStyleSheet("border:0px solid black;border-radius:10;background-color:#b0bfbf");//切换界面//会话界面en = new enter_widget;//交流界面com = new communication;connect(enter,&QPushButton::clicked,[=](){if(user_l->text()==QString("admin") && passwd_l->text() == "123456"){QMessageBox succbox(QMessageBox::NoIcon,"登录","登录成功",QMessageBox::Ok,enter);int err = succbox.exec();if(err == QMessageBox::Ok){this->close();com->show();}}else{int err = QMessageBox::critical(this,tr("错误"),tr("账号密码不匹配,是否重新登录"),QMessageBox::Yes | QMessageBox::Cancel,QMessageBox::Cancel);if(err == QMessageBox::Cancel){int ques = QMessageBox::question(this,tr("问题"),tr("是否退出"),QMessageBox::Yes | QMessageBox::No,QMessageBox::No);if(ques == QMessageBox::Yes){this->close();}}this->user_l->clear();this->passwd_l->clear();}});}
Widget::~Widget()
{
}