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

相关推荐
开发者小天14 小时前
python中calss的用法
开发语言·python
沉默-_-14 小时前
MyBatis 学习笔记
java·开发语言·tomcat
会游泳的石头14 小时前
构建企业级知识库智能问答系统:基于 Java 与 Spring Boot 的轻量实现
java·开发语言·spring boot·ai
m0_7482299914 小时前
Laravel4.x核心更新全解析
开发语言·php
j_xxx404_14 小时前
C++算法入门:滑动窗口合集(长度最小的子数组|无重复字符的最长字串|)
开发语言·c++·算法
m0_5613596714 小时前
C++中的过滤器模式
开发语言·c++·算法
HL_风神14 小时前
QT事件循环机制源码学习
开发语言·qt·学习
牵牛老人14 小时前
【Qt上位机与下位机交互数据组装与解析:全类型数据转换实战指南】
开发语言·qt·交互
郝学胜-神的一滴14 小时前
B站:从二次元到AI创新孵化器的华丽转身 | Google Cloud峰会见闻
开发语言·人工智能·算法
新缸中之脑14 小时前
Google:Rust实战评估
开发语言·后端·rust