思维导图
完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户迪纳基Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个
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 jump_slot();//第二个界面的槽函数声明
private:
Ui::Second *ui;
};
#endif // SECOND_H
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void my_jump();//第一个界面的信号声明
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
main.cpp:
#include "widget.h"
#include "second.h"//包含第二个界面的头文件
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//第一个界面实例化的窗口
Widget w;
//第二个界面实例化的窗口
Second s;
//连接
QObject::connect(&w,&Widget::my_jump,&s,&Second::jump_slot);
w.show();
return a.exec();
}
second.cpp:
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
}
Second::~Second()
{
delete ui;
}
void Second::jump_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->setWindowFlag(Qt::FramelessWindowHint);
}
Widget::~Widget()
{
delete ui;
}
//登录按钮对应的槽函数
void Widget::on_btn1_clicked()
{
if(ui->edit1->text() == "admin" && ui->edit2->text() == "123456")//如果账号和密码匹配,则弹出信息对话框
{
//弹出登录信息对话框,基于属性版本
QMessageBox msg(QMessageBox::Information,//图标
"提示信息",//标题
"登录成功",//文本
QMessageBox::Ok,//提供按钮
this);//指定父组件
//调用exec()弹出对话框
int ret = msg.exec();
if(ret == QMessageBox::Ok)
{
this->close();//关闭登录界面
//跳转到其他界面
emit my_jump();//触发第一个界面的信号
}
}
else//如果账号和密码不匹配,则弹出错误对话框
{
//弹出错误对话框,基于静态成员函数版本
int ret = QMessageBox::critical(this,
"错误",
"账号和密码不匹配,是否重新登录",
QMessageBox::Yes | QMessageBox::No);
if(ret == QMessageBox::Yes)
{
ui->edit2->clear();
}
else
{
this->close();
}
}
}
//取消按钮对应的槽函数
void Widget::on_btn2_clicked()
{
//用户点击取消按钮,弹出一个问题对话框,基于静态成员函数版本
int ret = QMessageBox::question(this,
"问题提示",
"您是否确定要退出登录?",
QMessageBox::Yes | QMessageBox::No);
if(ret == QMessageBox::Yes)
{
this->close();//关闭登录界面
}
}