2023.06.13 QT day2

完善登录界面
        1>点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
        2>如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
        3>点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
        要求:对象版和静态成员函数版至少各实现一个

窗口1头文件widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QCheckBox>
#include <QMessageBox>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QCheckBox *checkbox1;
    QCheckBox *checkbox2;

signals:
    void login_success();

public slots:
    void btn1_slot();
    void btn2_slot();
};

#endif // WIDGET_H

窗口2头文件secform.h

#ifndef SECFORM_H
#define SECFORM_H

#include <QWidget>
#include <QDebug>
#include <QLabel>
#include <QPushButton>
namespace Ui {
class SecForm;
}

class SecForm : public QWidget
{
    Q_OBJECT

public:
    explicit SecForm(QWidget *parent = nullptr);
    ~SecForm();

private:
    Ui::SecForm *ui;
    QLabel *label1;

public slots:
    void login_slot();
};

#endif // SECFORM_H

窗口1功能函数widget.cpp

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

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

    //窗口属性
    this->setFixedSize(480,360);
    this->setWindowTitle("QQ");
    this->setWindowIcon(QIcon(":/icon/QQ.png"));

    //登陆按钮
    btn1 = new QPushButton("登陆", this);
    btn1->resize(80,30);
    btn1->move(140,280);
    btn1->setIcon(QIcon(":/icon/login.png"));
    btn1->setStyleSheet("background-color:skyblue;"
                        "border-radius:10px;");

    //取消按钮
    btn2 = new QPushButton("取消", this);
    btn2->resize(80,30);
    btn2->move(240,280);
    btn2->setIcon(QIcon(":/icon/cancel.png"));
    btn2->setStyleSheet("background-color:skyblue;"
                        "border-radius:10px;");

    //账号密码行编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(200, 30);
    edit1->move(150, 170);
    edit1->setPlaceholderText("账号");
    edit2 = new QLineEdit(this);
    edit2->resize(200, 30);
    edit2->move(150, 205);
    edit2->setEchoMode(QLineEdit::Password);
    edit2->setPlaceholderText("密码");

    //图片标签
    label1 = new QLabel(this);
    label1->resize(480,160);
    label1->setScaledContents(true);
    label1->setPixmap(QPixmap(":/icon/pic2.jpg"));

    label2 = new QLabel(this);
    label2->resize(25,25);
    label2->move(120,172);
    label2->setScaledContents(true);
    label2->setPixmap(QPixmap(":/icon/user.png"));

    label3 = new QLabel(this);
    label3->resize(25,25);
    label3->move(120,207);
    label3->setScaledContents(true);
    label3->setPixmap(QPixmap(":/icon/password.png"));

    //复选框
    checkbox1 = new QCheckBox("自动登录", this);
    checkbox1->move(150,245);
    checkbox2 = new QCheckBox("记住密码", this);
    checkbox2->move(250,245);

    //QT4版本的连接
    connect(this->btn1, SIGNAL(clicked()), this, SLOT(btn1_slot()));
    //connect(this->btn2, SIGNAL(clicked()), this, SLOT(btn2_slot()));

    //QT5版本的连接
    connect(this->btn2, &QPushButton::clicked, this, &Widget::btn2_slot);
}

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

//登录按钮的槽函数
void Widget::btn1_slot()
{
    QMessageBox msgbox;
    int ret;
    if(edit1->text()=="admin" && edit2->text()=="123456")
    {
        msgbox.setIcon(QMessageBox::NoIcon);
        msgbox.setWindowTitle("QQ");
        msgbox.setText("登陆成功");
        msgbox.setStandardButtons(QMessageBox::Ok);
        msgbox.setDefaultButton(QMessageBox::Ok);
        msgbox.exec();
        emit login_success();
        this->close();
    }
    else if(edit1->text().isEmpty() || edit2->text().isEmpty())
    {
        msgbox.setIcon(QMessageBox::Warning);
        msgbox.setWindowTitle("QQ");
        msgbox.setText("账号或密码不能为空");
        msgbox.setStandardButtons(QMessageBox::Ok);
        msgbox.setDefaultButton(QMessageBox::Ok);
        msgbox.exec();
    }
    else
    {
        msgbox.setIcon(QMessageBox::Critical);
        msgbox.setWindowTitle("QQ");
        msgbox.setText("账号密码不匹配,是否重新登录?");
        msgbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgbox.setDefaultButton(QMessageBox::Ok);
        ret = msgbox.exec();
        if(ret == QMessageBox::Ok)
        {
            edit2->clear();
        }
        else if(ret == QMessageBox::Cancel)
        {
            msgbox.close();
        }
    }
}

//取消按钮的槽函数
void Widget::btn2_slot()
{
    QMessageBox::StandardButton ret = QMessageBox::question(this, "QQ", "确定是否退出?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
}

窗口1功能函数widget.cpp

#include "secform.h"
#include "ui_secform.h"

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

    //窗口属性
    this->setFixedSize(480,360);
    this->setWindowTitle("网络聊天室");
    this->setWindowIcon(QIcon(":/icon/QQ.png"));

}

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

void SecForm::login_slot()
{
    this->show();
}

主函数main.cpp

#include "widget.h"
#include "secform.h"
#include <QApplication>

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

    //实例化第二个窗口对象
    SecForm s;
    QObject::connect(&w, &Widget::login_success, &s, &SecForm::login_slot);

    return a.exec();
}

运行结果

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值