优化登录框:
当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录
当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面
当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面
要求:静态成员函数版本和对象版本各至少实现一个
头文件:lili.h
#ifndef LILI_H
#define LILI_H
#include <QWidget>
namespace Ui {
class lili;
}
class lili : public QWidget
{
Q_OBJECT
public:
explicit lili(QWidget *parent = nullptr);
~lili();
public slots:
void jump_slot()
{
this->show();
}
private:
Ui::lili *ui;
};
#endif // LILI_H
头文件:widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include<QLineEdit>
#include <QWidget>
#include<QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void jump();
private slots:
void my_slot();
void my_slot1();
private:
Ui::Widget *ui;
QLineEdit *edit1;
QLineEdit *edit2;
QPushButton *btn1;
QPushButton *btn2;
};
#endif // WIDGET_H
源文件:lili.cpp
#include "lili.h"
#include "ui_lili.h"
lili::lili(QWidget *parent) :
QWidget(parent),
ui(new Ui::lili)
{
ui->setupUi(this);
}
lili::~lili()
{
delete ui;
}
源文件:main.cpp
#include "widget.h"
#include "lili.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
lili l;
QObject::connect(&w,&Widget::jump,&l,&lili::jump_slot);
return a.exec();
}
源文件:widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QIcon>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("lili's window");
this->setWindowIcon(QIcon(":/tp/f54cccba4ab9b9122909fb0e0a4f3d3.jpg"));
this->setMaximumSize(800,600);
this->setMinimumSize(400,300);
this->setFixedSize(500,400);
//this->resize(800,600);
//this->set(QIcon("D:/BaiduNetdiskDownload/qt/yingyong/7adcf1a6baf7073e8434ae912bbb52a.png"));
this->setWindowOpacity(0.9);
this->move(400,300);
//QPainter a(this);
//a.drawPixmap(rect(),QPixmap("D:/BaiduNetdiskDownload/qt/yingyong/7adcf1a6baf7073e8434ae912bbb52a.png"),QRect());
QLabel *lab3=new QLabel(this);
lab3->resize(500,500);
lab3->setStyleSheet("background:skyblue;");
//QMovie *movie=new QMovie("");
//lab3->setScaledContents(true);
//lab3->setPixmap(QPixmap("D:/BaiduNetdiskDownload/qt/yingyong/218f6c69c7208efb13d66d558999dec.jpg"));
lab3->setPixmap(QPixmap(":/tp/1111.png"));
lab3->setScaledContents(true);
btn1=new QPushButton(QIcon(":/tp/12067205a1d2c773274d113ab9594f8.jpg"),"登录",this);
btn1->resize(60,30);
btn1->move(150,300);
btn1->setStyleSheet("color:white;background:pink;border-radius:10px;");
btn2=new QPushButton("注册",this);
btn2->resize(btn1->size());
btn2->move(btn1->x()+btn1->width()+70,btn1->y());
btn2->setStyleSheet("color:white;background:pink;border-radius:10px;");
btn2->setIcon(QIcon(":/tp/1359e195af6400e83b7490ddae6808f.jpg"));
QLabel *lab1=new QLabel("账号:",this);
lab1->resize(50,30);
lab1->move(btn1->x()-60,btn1->y()-90);
lab1->setStyleSheet("color:black;");
QLabel *lab2=new QLabel("密码:",this);
lab2->resize(50,30);
lab2->move(btn1->x()-60,lab1->y()+40);
lab2->setStyleSheet("color:black;");
edit1=new QLineEdit(this);
edit1->resize(250,30);
edit1->move(lab1->x()+lab1->width()+10,lab1->y());
edit1->clear();
edit1->setStyleSheet("color:pink");
edit1->setAlignment(Qt::AlignCenter);
edit1->setPlaceholderText("请输入账号");
edit2=new QLineEdit("密码",this);
edit2->resize(250,30);
edit2->move(edit1->x(),edit1->y()+40);
edit2->clear();
edit2->setStyleSheet("color:pink");
edit2->setAlignment(Qt::AlignCenter);
edit2->setPlaceholderText("请输入密码");
edit2->setEchoMode(QLineEdit::Password);
//connect(btn2,SIGNAL(clicked()),this,SLOT(close()));
connect(btn1,&QPushButton::clicked,this,&Widget::my_slot);
connect(btn2,&QPushButton::clicked,this,&Widget::my_slot1);
}
void Widget::my_slot()
{
QString username =edit1->text();
QString password =edit2->text();
if(username==password)
{
QMessageBox box(QMessageBox::Information,
"信息",
"登陆成功",
QMessageBox::Ok,
this);
box.setButtonText(QMessageBox::Ok,"确定");
box.setDefaultButton(QMessageBox::Ok);
int btn=box.exec();
if(btn==QMessageBox::Ok)
{
this->close();
emit jump();
}
}else{
int btn1=QMessageBox::warning(this,
"错误",
"账号和密码不相等,是否重新登陆",
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes);
if(btn1==QMessageBox::Yes)
{
edit2->clear();
}else if(btn1==QMessageBox::No)
{
this->close();
}
}
}
void Widget::my_slot1()
{
QMessageBox box(QMessageBox::Question,
"error",
"是否退出登录",
QMessageBox::Yes|QMessageBox::No,
this);
box.setDefaultButton(QMessageBox::Yes);
int btn=box.exec();
if(btn==QMessageBox::Yes)
{
this->close();
}
}
Widget::~Widget()
{
delete ui;
}