QTday3对话框

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>  //标签类头文件
#include <QPushButton> //按钮类头文件
#include <QLineEdit>  //行编辑器类
#include <QMessageBox> //消息对话框

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public slots:
    void clicked_slot(); // 登录按钮的槽函数
    void cancel_slot();  // 取消按钮的槽函数

signals:
    void jump(); // 跳转信号

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    QLabel *label;          // logo
    QLabel *label_2;        // 用户名标签
    QLabel *label_3;        // 密码标签
    QLineEdit *lineEdit;    // 账号输入框
    QLineEdit *lineEdit_2;  // 密码输入框
    QPushButton *pushButton; // 登录按钮
    QPushButton *pushButton_2; // 取消按钮

    // 静态成员函数版本
    static void showSuccessMessage(); // 登录成功的静态提示
    static bool showErrorMessage();   // 登录失败的静态提示
};

#endif // WIDGET_H

 第二界面头文件

#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT
public slots:
    void jump_slot();  //界面2提供的槽函数
public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();

private:
    Ui::Form *ui;
};

#endif // FORM_H

widget.cpp

#include "widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(400, 560);

    // 初始化控件
    label = new QLabel(this);
    label_2 = new QLabel(this);
    label_3 = new QLabel(this);
    lineEdit = new QLineEdit(this);
    lineEdit_2 = new QLineEdit(this);
    pushButton = new QPushButton(this);
    pushButton_2 = new QPushButton(this);

    // 对logo进行设置
    label->setGeometry(0, 0, 400, 200); // 设置位置和大小
    label->setPixmap(QPixmap(":/pictrue/logo.png"));
    label->setScaledContents(true);

    // 给账号密码的标签设置图像
    label_2->setPixmap(QPixmap(":/pictrue/userName.jpg"));
    label_2->setScaledContents(true);
    label_2->setGeometry(50, 250, 60, 30); // 设置位置和大小

    label_3->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
    label_3->setScaledContents(true);
    label_3->setGeometry(50, 300, 60, 30); // 设置位置和大小

    // 对账号密码输入框进行设置
    lineEdit->setPlaceholderText("请输入账号"); // 设置占位文本
    lineEdit->setAlignment(Qt::AlignCenter);
    lineEdit->setGeometry(150, 250, 200, 30); // 设置位置和大小

    lineEdit_2->setPlaceholderText("请输入密码"); // 设置占位文本
    lineEdit_2->setAlignment(Qt::AlignCenter);
    lineEdit_2->setEchoMode(QLineEdit::Password); // 设置回显模式
    lineEdit_2->setGeometry(150, 300, 200, 30); // 设置位置和大小

    // 给按钮设置图标
    pushButton->setIcon(QIcon(":/pictrue/login.png"));
    pushButton->setGeometry(100, 400, 100, 50); // 设置位置和大小
    pushButton->setText("登录");

    pushButton_2->setIcon(QIcon(":/pictrue/cancel.png"));
    pushButton_2->setGeometry(200, 400, 100, 50); // 设置位置和大小
    pushButton_2->setText("取消");

    // 将登录按钮的clicked信号连接到自定义的槽函数中
    connect(pushButton, &QPushButton::clicked, this, &Widget::clicked_slot);

    // 将取消按钮的clicked信号连接到自定义的槽函数中
    connect(pushButton_2, &QPushButton::clicked, this, &Widget::cancel_slot);
}

Widget::~Widget()
{
    // 不需要手动释放控件,Qt 会自动管理
}

// 登录按钮的点击信号对应的槽函数
void Widget::clicked_slot()
{
    // 获取账号和密码
    QString usrname = lineEdit->text();
    QString pwd = lineEdit_2->text();

    if (usrname == pwd)
    {
        // 登录成功,弹出信息对话框
        showSuccessMessage(); // 静态成员函数版本
        this->close(); // 关闭当前界面
        emit jump();   // 发射跳转信号
    }
    else
    {
        // 登录失败,弹出错误对话框
        bool retry = showErrorMessage(); // 静态成员函数版本
        if (retry)
        {
            lineEdit_2->clear(); // 清空密码框
        }
        else
        {
            this->close(); // 关闭登录界面
        }
    }
}

// 取消按钮的点击信号对应的槽函数
void Widget::cancel_slot()
{
    // 弹出问题对话框
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "退出登录", "确定要退出登录吗?",
                                  QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes)
    {
        this->close(); // 关闭登录界面
    }
    // 如果选择 No,则不做任何操作
}

// 静态成员函数版本:显示登录成功的消息
void Widget::showSuccessMessage()
{
    QMessageBox::information(nullptr, "登录成功", "登录成功!", QMessageBox::Ok);
}

// 静态成员函数版本:显示登录失败的消息
bool Widget::showErrorMessage()
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(nullptr, "登录失败", "账号和密码不匹配,是否重新登录?",
                                  QMessageBox::Yes | QMessageBox::No);
    return (reply == QMessageBox::Yes); // 返回用户是否选择重新登录
}

form.cpp

#include "form.h"
#include "ui_form.h"

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

Form::~Form()
{
    delete ui;
}
//界面2的接收信号槽函数实现
void Form::jump_slot()
{
    this->show();
}

main.cpp

#include "widget.h"

#include"form.h"
#include <QApplication>

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

    Form s;
    //将两个界面信号与槽进行连接
    QObject::connect(&w,  //第一个界面发射信号
                &Widget::jump,  //发射的是跳转信号
                &s,
            &Form::jump_slot);
    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值