今日任务:
1.
完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到新的界面中
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个
要求:尽量每行代码都有注释
2.思维导图
文件目录:
代码:
page2.h:
#ifndef PAGE2_H
#define PAGE2_H
#include <QWidget>
#include <QMovie>
namespace Ui {
class Page2;
}
class Page2 : public QWidget
{
Q_OBJECT
public:
explicit Page2(QWidget *parent = nullptr);
~Page2();
public slots:
void login_slot();
private:
Ui::Page2 *ui;
};
#endif // PAGE2_H
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMovie>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void loginButton_slot();
signals:
void login_signal();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
main.cpp:
#include "widget.h"
#include "page2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Page2 p2;
QObject::connect(&w,&Widget::login_signal,&p2,&Page2::login_slot);
return a.exec();
}
page2.cpp:
#include "page2.h"
#include "ui_page2.h"
Page2::Page2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Page2)
{
ui->setupUi(this);
QMovie *movie = new QMovie(":/111/cai.gif");
ui->label->setMovie(movie);
ui->label->setScaledContents(true);
movie->start();
}
Page2::~Page2()
{
delete ui;
}
void Page2::login_slot()
{
this->show();
}
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(560,430);
this->setStyleSheet("background-color:#faf7ec");
this->setWindowFlag(Qt::FramelessWindowHint);//无边框
QMovie *movie = new QMovie(":/111/cai.gif");
ui->backLabel->setMovie(movie);
ui->backLabel->setScaledContents(true);
movie->start();
ui->closeButton->setStyleSheet("border-image:url(:/111/basketball.png)");
ui->avatorLabel->resize(60,60);
ui->avatorLabel->setStyleSheet("border-image:url(:/111/user.png);border-radius:30px");
ui->accountLabel->setPixmap(QPixmap(":/111/account.jpg"));
//ui->accountLabel->resize(40,40);
ui->accountLabel->setScaledContents(true);
ui->passwdLabel->setPixmap(QPixmap(":/111/passwd.jpg"));
//ui->passwdLabel->resize(40,40);
ui->passwdLabel->setScaledContents(true);
ui->accoountLine->setPlaceholderText("账号");
ui->passwdLine->setPlaceholderText("密码");
ui->passwdLine->setEchoMode(QLineEdit::Password);
ui->loginLabel->setPixmap(QPixmap(":/111/2.png"));
ui->loginLabel->setScaledContents(true);
ui->loginButton->setStyleSheet("background-color:#409EFF;border-radius:5px");
connect(ui->closeButton,SIGNAL(clicked()),this,SLOT(close()));
connect(ui->loginButton,&QPushButton::clicked,this,&Widget::loginButton_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::loginButton_slot()
{
if(ui->accoountLine->text()=="admin"&&ui->passwdLine->text()=="123456"){
qDebug() << "登录成功" <<endl;
QMessageBox::information(this,"提示","登录成功",QMessageBox::Ok);
this->close();
//开启新窗口
emit login_signal();
}else{
qDebug() << "账号或者密码错误" <<endl;
int res=QMessageBox::information(this,"提示","账号或者密码错误,是否继续登录",QMessageBox::Ok|QMessageBox::No);
if(res==QMessageBox::Ok){
ui->passwdLine->setText("");
}else{
this->close();
}
}
}
运行结果:
今日思维导图: