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

相关推荐
李白同学1 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?2 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农2 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿2 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风3 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
数巨小码人3 小时前
QT SQL框架及QSqlDatabase类
jvm·sql·qt
dorabighead3 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
风与沙的较量丶4 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
水煮庄周鱼鱼4 小时前
C# 入门简介
开发语言·c#
编程星空5 小时前
css主题色修改后会多出一个css吗?css怎么定义变量?
开发语言·后端·rust