1.思维导图
2.完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示“登录成功”,提供一个OK按钮,用户点击OK后,关闭登录界面,跳转到其他界面。
如果账号和密码不匹配,弹出错误对话框,给出信息“账号和密码不匹配,是否重新登录”,并提供两个按钮Yes/No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面。
如果用户点击取消按钮,则弹出一个问题对话框,给出信息“您是否确定要退出登录?”,并给出两个按钮Yes/No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能。
要求:基于属性版和基于静态成员函数版至少各用一个。
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QIcon>
#include <QWidget>
#include <QPixmap>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
#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_pushButton_clicked();
void on_btn2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口标题
this->setWindowTitle("酒店信息管理系统");
//设置窗口图标
this->setWindowIcon(QIcon(":/pictrue/xiaoxiong.gif"));
//重新设置大小
this->resize(480,500);
//窗口上半部分设置动图
ui->Logolab->setPixmap(QPixmap(":/pictrue/xiaoxiong.gif"));
ui->Logolab->setScaledContents(true);
//给账户标签设置图标
ui->Username->setPixmap(QPixmap(":/pictrue/wodepeizhenshi.png"));
ui->Username->setScaledContents(true);
//给密码标签设置图标
ui->Passwd->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
ui->Username->setScaledContents(true);
//给账户的行编辑器设置输入提示
ui->UserlineEdit->setPlaceholderText("手机/邮箱");
ui->PasswdlineEdit->setEchoMode(QLineEdit::Password);
ui->PasswdlineEdit->setPlaceholderText("密码");
//手动链接信号与自定义槽函数,基于qt4版本连接
connect(ui->btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));
//手动链接信号与自定义槽函数,基于qt5版本连接
}
Widget::~Widget()
{
delete ui;
}
//取消按钮槽函数
void Widget::on_pushButton_clicked()
{
//使用静态函数实现,弹出问题对话框
int ret=QMessageBox::question(this,"问题","您是否确定要退出登录?",QMessageBox::Yes | QMessageBox::No);
if(ret==QMessageBox::Yes)
{
this->close();// 关闭窗口
}
}
//登录按钮自定义槽函数
void Widget::on_btn2_clicked()
{
if(ui->UserlineEdit->text()=="admin" && ui->PasswdlineEdit->text()=="123456")
{
//弹出一个信息对话框
QMessageBox msg(QMessageBox::Information,"信息对话框","登录成功",QMessageBox::Yes | QMessageBox::No,this);
//调用exec()弹出对话框
int ret=msg.exec();
//根据用户选择的按钮,执行不同的功能
if(ret==QMessageBox::Yes)
{
//qDebug() << "登录成功";
this->close();
}
}
else
{
//使用静态成员实现,弹出一个错误对话框
int ret=QMessageBox::critical(this,"错误","账号和密码不匹配,是否重新登录",QMessageBox::Yes | QMessageBox::No);
if(ret==QMessageBox::Yes)
{
ui->UserlineEdit->clear();
}
else
{
this->close();
}
qDebug() << "登录失败";
ui->PasswdlineEdit->clear();
}
}
运行结果: