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

相关推荐
ALex_zry5 分钟前
让 Python 脚本在后台持续运行:架构级解决方案与工业级实践指南
开发语言·python·架构
alive9031 小时前
【QT】 进程
c++·qt·嵌入式·进程·qprocess
Blood_J2 小时前
python网络爬虫
开发语言·爬虫·python
xiaowu0803 小时前
C# task任务异步编程提高UI的响应性
开发语言·c#
kill bert5 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
低头专研7 小时前
Markdown标题序号处理工具——用 C 语言实现
c语言·开发语言·typora·markdown文件标题编号·md文件标题序号
刚入门的大一新生8 小时前
C++初阶-C++入门基础
开发语言·c++
你是理想8 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
forestsea9 小时前
Python进阶编程总结
开发语言·python·notepad++
q567315239 小时前
使用Java的HttpClient实现文件下载器
java·开发语言·爬虫·scrapy