作业:
完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户迪纳基Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QIcon>
#include <QMovie>
#include <QLineEdit>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_cancelBtn_clicked();
void on_loginBtn_clicked();
signals:
void my_jump();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
public slots:
void my_jump_slot();
private:
Ui::Second *ui;
};
#endif // SECOND_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//窗口
this->setWindowTitle("DARK SOULS III");
this->setWindowIcon(QIcon(":/Ash/R-C.jpg"));
this->setWindowFlag(Qt::FramelessWindowHint);
//标签
ui->logoLab->setPixmap(QPixmap(":/Ash/DS.png"));
ui->logoLab->setScaledContents(true);
ui->logoLab_2->setPixmap(QPixmap(":/Ash/Ash.png"));
ui->logoLab_2->setScaledContents(true);
ui->userNameLab->setPixmap(QPixmap(":/Ash/us.png"));
ui->userNameLab->setScaledContents(true);
ui->passwdLab->setPixmap(QPixmap(":/Ash/pw.png"));
ui->passwdLab->setScaledContents(true);
//行编辑
ui->passwdEdit->setEchoMode(QLineEdit::Password);
ui->userNameEdit->setPlaceholderText("账号");
ui->passwdEdit->setPlaceholderText("密码");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_cancelBtn_clicked()
{
int ret = QMessageBox::question(this,
"问题",
"是否退出",
QMessageBox::Yes | QMessageBox::No);
if(ret == QMessageBox::Yes)
{
this->close();
}
}
void Widget::on_loginBtn_clicked()
{
if(ui->userNameEdit->text() == "zxr" && ui->passwdEdit->text() == "123456")
{
QMessageBox box(
QMessageBox::Information,
"信息",
"登录成功",
QMessageBox::Ok);
int ret = box.exec();
if(ret == QMessageBox::Ok)
{
this->close();
emit my_jump();
}
}
else
{
int ret2 = QMessageBox::warning(this,
"警告",
"密码与账号不匹配",
QMessageBox::Yes | QMessageBox::No);
if(ret2 == QMessageBox::Yes)
{
ui->userNameEdit->setText("");
ui->passwdEdit->setText("");
}
else
{
this->close();
}
}
}
second.cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
ui->label->setPixmap(QPixmap(":/Ash/SOUL.jpg"));
ui->label->setScaledContents(true);
}
Second::~Second()
{
delete ui;
}
void Second::my_jump_slot()
{
this->show();
}
main.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Second s;
QObject::connect(&w , &Widget::my_jump , &s , &Second::my_jump_slot);
return a.exec();
}
运行结果:
取消:
登录失败:
登录成功: