完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到新的界面中
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个
要求:尽量每行代码都有注释
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QDebug>
#include <QString>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
public slots:       //表示该权限下都是公共的槽函数
    void  cancelBtn_slots();   //自定义一个槽函数
    void loginBtn_slot();
signals:
    void jump();  //自定义一个跳转信号
private:
    Ui::MyWidget *ui;
};
#endif // MYWIDGET_H
second.h
#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 jumpSlot();  //对应第一个窗口信号的槽函数声明
private:
    Ui::Second *ui;
};
#endif // SECOND_H
main.cpp
#include "mywidget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    w.show();
    //实例化第二个窗口的对象
    Second s;
    //将第一个窗口的jump信号和第二窗口的jumpslot槽函数连接
    QObject::connect(&w,&MyWidget::jump,&s,&Second::jumpSlot);
    return a.exec();
}
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    //窗口相关设置
    this->setWindowTitle("QQ聊天");
    this->setWindowIcon(QIcon(":/pictrue/qq.png"));
    this->setFixedSize(601,394);
    //logo设置
    ui->logoLab->setPixmap(QPixmap(":/pictrue/logo.png"));
    ui->logoLab->setScaledContents(true);
    //账号
    ui->userNameLab->resize(30,30);
    ui->userNameLab->setPixmap(QPixmap(":/pictrue/wodepeizhenshi.png"));
    ui->userNameLab->setScaledContents(true);
    ui->userNameEdit->setPlaceholderText("QQ账号/手机/邮箱");
    //密码
    ui->passwordLab->resize(30,30);
    ui->passwordLab->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
    ui->passwordLab->setScaledContents(true);
    ui->passwordEdit->setEchoMode(QLineEdit::Password);
    //登录和取消
    ui->loginBtn->setIcon(QIcon(":/pictrue/login.png"));
    ui->cancelBtn->setIcon(QIcon(":/pictrue/cancel.png"));
    //手动连接信号和系统槽,基于qt4版本    是不友好的连接
    //取消   利用槽函数点击实现关闭
     this->connect(ui->cancelBtn,SIGNAL(clicked()),this,SLOT(cancelBtn_slots()));
     //手动连接信号和系统槽,基于qt5版本    是友好的连接
     connect(ui->loginBtn,&QPushButton::clicked,this,&MyWidget::loginBtn_slot);
}
MyWidget::~MyWidget()
{
    delete ui;
}
void MyWidget::cancelBtn_slots()
{
//    //用消息对话框   实例化一个对象  属性版
//    QMessageBox msg(
//                QMessageBox::Question,   //图标
//                "问题",   //标题
//                "您是否确定要退出登录?",   //文本
//                QMessageBox::Yes | QMessageBox::No,    //提供按钮
//                this);   //父对象
//    //用 exec() 执行对话框
//    int ret = msg.exec();
//    //对用户选中的按钮进行判断
//    if(ret == QMessageBox::Yes)
//    {
//        this->close();
//    }
//    else
//    {
//        msg.close();
//    }
    //用消息对话框  静态成员函数版
    int ret = QMessageBox::question(this,  //父对象
                                    "问题",   //标题
                                    "您是否确定要退出登录?",   //文本
                                    QMessageBox::Yes | QMessageBox::No);    //提供按钮
    //对用户选中的按钮进行判断
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
    else
    {
        return;
    }
}
void MyWidget::loginBtn_slot()
{
    if(ui->userNameEdit->text() == "admin" && ui->passwordEdit->text() == "123456")
    {
        //用消息对话框   实例化一个对象   属性版
//        QMessageBox msg(
//                    QMessageBox::Information,   //图标
//                    "提示",   //标题
//                    "登录成功",   //文本
//                    QMessageBox::Ok,    //提供按钮
//                    this);   //父对象
        //用 exec() 执行对话框
//        msg.exec();
        //用消息对话框  静态成员函数版
       QMessageBox::question(this,  //父对象
                             "提示",   //标题
                             "登录成功",   //文本
                             QMessageBox::Ok);   //提供按钮
        this->close();
        emit jump();
        qDebug("%s\n","登录成功");
        this->close();
    }
    else
    {
//        //用消息对话框   实例化一个对象   属性版
//            QMessageBox msg(
//                        QMessageBox::Critical,   //图标
//                        "错误",   //标题
//                        "账号和密码不匹配,是否重新登录",   //文本
//                        QMessageBox::Yes | QMessageBox::No,    //提供按钮
//                        this);   //父对象
//            //用 exec() 执行对话框
//           int ret = msg.exec();
//           //对用户选中的按钮进行判断
//          if(ret == QMessageBox::Yes)
//          {
//              ui->passwordEdit->clear();
//          }
//          else
//          {
//              this->close();
//          }
        //用消息对话框  静态成员函数版
        int ret = QMessageBox::critical(this,  //父对象
                                        "错误",   //标题
                                        "账号和密码不匹配,是否重新登录",   //文本
                                        QMessageBox::Yes | QMessageBox::No);    //提供按钮
        //对用户选中的按钮进行判断
        if(ret == QMessageBox::Yes)
        {
            ui->passwordEdit->clear();
        }
        else
        {
            this->close();
        }
        qDebug("%s\n","登录失败");
    }
}
second.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::jumpSlot()
{
    this->show();
}
思维导图




















