12月11日作业

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

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

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

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

第一个头文件

#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();

private slots:
    void on_btn_log_clicked();

    void on_btn_cancle_clicked();

private:
    Ui::Widget *ui;
signals:
    void my_jump();  //第一个界面的自定义信号

};
#endif // WIDGET_H

第一个源文件

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    this->setWindowIcon(QIcon(":/pictrue/kunkun.webp"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btn_log_clicked()  //登录按钮槽函数
{
    //静态成员函数版本
    if(ui->edit_id->text() == "admin" && ui->edit_passward->text() == "123456")
    {
        int ret = QMessageBox::information(this,
                                           "登陆成功",
                                           "确认登录");

        if(ret == QMessageBox::Ok)
        {
            //跳转到其他界面
            emit my_jump();  //触发自定义信号
            //关闭当前界面
            this->close();
        }
    }
    else  //登录失败
    {
        //静态成员函数版本
        int ret = QMessageBox::critical(this,
                                        "登录失败",
                                        "账号和密码不匹配,是否重新登录",
                                        QMessageBox::Yes | QMessageBox::No);

        if(ret == QMessageBox::Yes)
        {
            //清空密码,重新登录
            ui->edit_passward->clear();
        }
        else if(ret == QMessageBox::No)
        {
            //直接关闭
            this->close();
        }
    }
}

void Widget::on_btn_cancle_clicked()  //取消按钮槽函数
{
    //基于属性版本
    QMessageBox msg(QMessageBox::Question,
                    "取消",
                    "您是否确定要取消登录?",
                    QMessageBox::Yes | QMessageBox::No,
                    this);
    //调用exec函数
    int ret = msg.exec();
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
}

第二个头文件

#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();

private:
    Ui::Second *ui;

public slots:
    void jump_slot();  //自定义的槽函数说明
};

#endif // SECOND_H

第二个源文件

#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();
}

ui文件

效果演示

君子声明

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值