QT-day2

1、完善登录框

头文件

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMessageBox>      //消息对话框类头文件
#include <QDebug>
#include <QPushButton>
#include "second.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

signals:
    void my_signal();       //定义一个无参无返回值的信号函数
    void jump();            //自定义跳转信号函数
private slots:
    void my_slot();         //自定义无参无返回值槽函数

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:

    void on_dengBtn_clicked();

    void on_quBtn_clicked();

private:
    Ui::Widget *ui;
    second *s1;
};
#endif // WIDGET_H

second.h

#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class second;
}

class second : public QWidget
{
    Q_OBJECT

public slots:
    void jump_slot();           //接收跳转信号的槽函数

public:
    explicit second(QWidget *parent = nullptr);
    ~second();

private:
    Ui::second *ui;
};

#endif // SECOND_H

源文件

widget.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    s1 = new second;
    //使用qt5版本,把登录、取消按钮连接
    connect(ui->dengBtn, &QPushButton::clicked, this, &Widget::my_slot);
    connect(ui->quBtn, &QPushButton::clicked, this, &Widget::my_slot);
    connect(this, &Widget::jump, s1, &second::jump_slot);
}

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

//登录按钮对应的槽函数
void Widget::on_dengBtn_clicked()
{
    //传入账号密码
    QString zh = ui->lineEdit1->text();
    QString mm = ui->lineEdit2->text();

    if("admin"!=zh || "123456"!=mm)
    {
        //1、调用构造函数实例化对象
        QMessageBox box(QMessageBox::Critical,               //图标
                        "错误",                                  //对话框标题
                        "账号密码不匹配,是否重新登录",                             //对话框文本内容
                        QMessageBox::Ok | QMessageBox::Cancel,     //提供的按钮
                        this);                                  //父组件
        //box.setDetailedText("");               //提供详细文本内容
        box.setDefaultButton(QMessageBox::Ok);         //将yes设置成默认按钮

        //2、调用exec函数运行对话框
        int ret = box.exec();

        //3、对结果进行判断
        if(ret == QMessageBox::Ok)
        {
            ui->lineEdit1->clear();
            ui->lineEdit2->clear();
            qDebug()<<"继续登录";
        }else if(ret == QMessageBox::Cancel)
        {
            close();
        }
    }else if("admin"==zh && "123456"==mm)
    {
        //1、调用构造函数实例化对象
        QMessageBox box(QMessageBox::Information,               //图标
                        "信息",                                 //对话框标题
                        "登陆成功",                       //对话框文本内容
                        QMessageBox::Ok,               //提供的按钮
                        this);                          //父组件
        //box.setDetailedText("");                    //提供详细文本内容
        box.setDefaultButton(QMessageBox::Ok);         //将Ok设置成默认按钮

        //2、调用exec函数运行对话框
        int ret = box.exec();

        //3、对结果进行判断
        if(ret == QMessageBox::Ok)
        {
            emit jump();
        }
    }

}

//取消
void Widget::on_quBtn_clicked()
{
    //1、调用静态函数实例化对象
    int ret = QMessageBox::information(this,              //父组件
                         "问题",              //对话框标题
                         "是否确定退出登录",            //对话框文本内容
                         QMessageBox::Yes | QMessageBox::No,    //对话框提供的按钮
                         QMessageBox::No);          //默认选中的按钮

    //3、对结果进行判断
    if(ret == QMessageBox::Yes)
    {
        close();
    }
}

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

主程序

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    
    return a.exec();
}

2、思维导图

猜你喜欢

转载自blog.csdn.net/Lychee_z23/article/details/132996211