完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示"登录成功”,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息"账号和密码不匹配,是否重新登录"并提供两个按钮Yes/No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
要求:基于属性版和基于静态成员函数版至少各用一个
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#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();
//第一个界面自定义信号
signals:
void my_jump();
private slots:
void on_pushButton_clicked();
void on_Btn2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_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
源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);//去掉头部
this->setAttribute(Qt::WA_TranslucentBackground);//去掉窗口剩余部分
this->setWindowIcon(QIcon(":/pictrue/qq.png"));//设置图标
}
Widget::~Widget()
{
delete ui;
}
//登录按钮对应的槽函数处理
void Widget::on_pushButton_clicked()
{
//判断登录是否成功
if(ui->userNameEdit->text() == "admin" && ui->passwdEdit->text() == "123456")
{
//信息对话框 基于静态函数版本
int ret = QMessageBox::information(this,"信息","登录成功",QMessageBox::Yes);
if(ret == QMessageBox::Yes)
{
emit my_jump();//触发信号
this->close();//关闭第一个界面
}
}else
{
//错误对话框 基于属性版本
QMessageBox msg(QMessageBox::Critical,"错误","账号和密码不匹配,是否重新登陆",QMessageBox::Yes | QMessageBox::No,this);
int ret = msg.exec();//弹出对话框
if(ret == QMessageBox::Yes)
{
ui->passwdEdit->clear();//清空密码
}else if(ret == QMessageBox::No)
{
this->close();//关闭页面
}
}
}
//取消按钮对应的槽函数处理
void Widget::on_Btn2_clicked()
{
//问题对话框 基于静态函数版本
int ret = QMessageBox::question(this,"问题","您是否确定要退出登录?",QMessageBox::Yes | QMessageBox::No);
if(ret == QMessageBox::Yes)
{
this->close();//关闭登录页面
}
}
#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();
}
主函数
#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::jump_slot);//连接两个界面
return a.exec();
}