2024.3.15

作业:

第一个界面头文件:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QString>
#include <QDebug>
#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 jump_signal();

private slots:
    void on_btn2_clicked();
    void my_slot();
    void bt1_slot();



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

第一个界面源文件:

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //去掉头部
    this->setWindowFlag(Qt::FramelessWindowHint);
    //去掉空白的地方
    this->setAttribute(Qt::WA_TranslucentBackground);
    //连接按钮2的信号和槽函数 基于qt4版本
    connect(ui->btn2,SIGNAL(clicked()),this,SLOT(my_slot()));
    //连接信号和自定义槽函数,基于qt5版本的连接
    connect(ui->btn1,&QPushButton::clicked,this,&Widget::bt1_slot);


}

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




void Widget::on_btn2_clicked()
{

}

void Widget::my_slot()
{
    QMessageBox msg(QMessageBox::Warning,"警告","确定要取消登录吗?",QMessageBox::Yes|QMessageBox::No);

    int ret = msg.exec();

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

    }
}

void Widget::bt1_slot()
{
    QString str1 = "admin";
    QString str2 = ui->usenameEdit->text();
    QString passwd = "123456";
    QString passwd2 = ui->passwdEdit->text();
    if(str1 == str2 && passwd == passwd2)
    {
       QMessageBox::information(this,"","登录成功!");
       this->close();
       emit jump_signal();
    }
    else
    {
        int ret = QMessageBox::question(this,"","用户名或密码错误",QMessageBox::Yes|QMessageBox::No);
        if(ret == QMessageBox::Yes)
        {
            ui->passwdEdit->clear();
        }
        else
        {
            this->close();
        }

    }


}

第二个界面头文件:

#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

第二个界面源文件:

#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 "second.h"

#include <QApplication>


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

    Second s;

    QObject::connect(&w, &Widget::jump_signal, &s, &Second::jump_slot);
    return a.exec();
}

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值