QT(3/25)

时间:2024-03-29 18:47:14

完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示“登录成功”,提供一个OK按钮,用户点击OK后,关闭登录界面,跳转到其他界面。

如果账号和密码不匹配,弹出错误对话框,给出信息“账号和密码不匹配,是否重新登录”,并提供两个按钮Yes/No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面。

如果用户点击取消按钮,则弹出一个问题对话框,给出信息“您是否确定要退出登录?”,并给出两个按钮Yes/No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能。

要求:基于属性版和基于静态成员函数版至少各用一个。

#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);
 
    connect(ui->loginBtn, &QPushButton::clicked, this, &Widget::loginBtn);
}
 
// 登录按钮
void Widget::loginBtn()
{
    QString username = ui->lineEdit1->text();
    QString password = ui->lineEdit2->text();
 
    if (username == "admin" && password == "123456")
    {
        //基于静态函数的消息对话框
        int ret = QMessageBox::information(this,
                                 "消息提示",
                                 "登录成功",
                                 QMessageBox::Ok);
        //如果收到OK回复,跳转窗口
        if(ret == QMessageBox::Ok)
        {
            emit my_jump_signal();
        }
 
        this->close(); // 关闭窗口
    }
    else
    {
        //基于属性的消息对话框
        QMessageBox msg(
                    QMessageBox::Critical,
                    "错误",
                    "账号和密码不匹配,是否重新登录",
                    QMessageBox::Yes | QMessageBox::No,
                    this);
        int ret = msg.exec();//基于属性的对话框需要用exec获取返回
        if(ret == QMessageBox::Yes)
        {
            ui->lineEdit2->clear(); // 清空密码框内容
        }else{
            this->close();
        }
 
    }
}
//取消按钮
void Widget::on_cancelBtn_clicked()
{
    //基于静态函数的消息对话框
    int ret = QMessageBox::question(
                this,
                "问题",
                "是否退出登录",
                QMessageBox::Yes | QMessageBox::No);
 
    if(ret == QMessageBox::No)
    {
 
    }else{
        this->close();
    }
}
 
Widget::~Widget()
{
    delete ui;
}