QT对话框作业

widget.h

cpp 复制代码
#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();

signals:
    void my_jump();
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

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

second.h

cpp 复制代码
#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

main.cpp

cpp 复制代码
#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();
}

widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

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

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


void Widget::on_pushButton_clicked()
{
    QString username = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();

    if (username == "admin" && password == "123456")
    {
        QMessageBox msg(QMessageBox::Information,"登录","登录成功",QMessageBox::Ok,this);

        int ret = msg.exec();

        if(ret == QMessageBox::Ok)
        {
            this->close();
            emit my_jump();
        }
    }
    else
    {
        QMessageBox msg(QMessageBox::Critical,"错误","账号和密码不匹配,是否重新登陆",QMessageBox::Yes|QMessageBox::No);

        int ret = msg.exec();

        if(ret == QMessageBox::Yes)
        {
            ui->lineEdit_2->clear();
        }
        else
        {
            this->close();
        }
    }
}

void Widget::on_pushButton_2_clicked()
{
    QMessageBox msg(QMessageBox::Question,"问题","您是否要退出登录?",QMessageBox::Yes|QMessageBox::No);

    int ret = msg.exec();

    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
    else
    {
        return;
    }
}

second.cpp

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

相关推荐
superxxd1 小时前
跨平台音频IO处理库libsoundio实践
开发语言·qt·音视频
ajassi20002 小时前
开源 C++ QT QML 开发(二十)多媒体--摄像头拍照
c++·qt·开源
_OP_CHEN2 小时前
C++基础:(十二)list类的基础使用
开发语言·数据结构·c++·stl·list类·list核心接口·list底层原理
ONE_PUNCH_Ge5 小时前
Go 语言变量
开发语言
幼稚园的山代王5 小时前
go语言了解
开发语言·后端·golang
晚风残5 小时前
【C++ Primer】第六章:函数
开发语言·c++·算法·c++ primer
满天星83035776 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
weixin_456904276 小时前
基于.NET Framework 4.0的串口通信
开发语言·c#·.net
ss2736 小时前
手写MyBatis第107弹:@MapperScan原理与SqlSessionTemplate线程安全机制
java·开发语言·后端·mybatis
麦麦鸡腿堡7 小时前
Java的动态绑定机制(重要)
java·开发语言·算法