思维导图

QT-Day3-LMLPHP

作业

完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户迪纳基Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个

widget.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    //设置背景
    ui->setupUi(this);
    ui->lab1->setPixmap(QPixmap(":/pictrue/nxd.jpg"));
    ui->lab1->setScaledContents(true);

    //设置用户名和密码
    ui->Namelab->setPixmap(QPixmap(":/pictrue/ls.jpg"));
    ui->Namelab->setScaledContents(true);
    ui->NamelineEdit->setPlaceholderText("uid");
    ui->PassWdlab->setPixmap(QPixmap(":/pictrue/nl.jpg"));
    ui->PassWdlab->setScaledContents(true);
    ui->PassWdlineEdit->setPlaceholderText("password");
    ui->PassWdlineEdit->setEchoMode(QLineEdit::Password);

    //设置透明度
    ui->PassWdlineEdit->setStyleSheet("background-color: rgba(255, 255, 255, 128);");
    ui->NamelineEdit->setStyleSheet("background-color: rgba(255, 255, 255, 128);");
    ui->Btn1->setStyleSheet("background-color: rgba(255, 255, 255, 128);");
    ui->Btn2->setStyleSheet("background-color: rgba(255, 255, 255, 128);");

    //设置两个动图
    QMovie *mv = new QMovie(":/pictrue/preview.gif");
    mv->start();
    ui->giflab1->setMovie(mv);
    ui->giflab2->setMovie(mv);
    ui->giflab1->setScaledContents(true);
    ui->giflab2->setScaledContents(true);

    //手动连接到自定义的槽函数中实现关闭窗口
    connect(ui->Btn2,SIGNAL(clicked()),this,SLOT(myslots()));

    connect(ui->Btn1,&QPushButton::click,this,&Widget::on_Btn1_clicked);


}

void Widget::myslots()
{
    QMessageBox msg(QMessageBox::Question,
                    "question",
                    "Are you sure you want to log out and log in",
                    QMessageBox::Yes|QMessageBox::No,
                    this);
    int res = msg.exec();
    if(res==QMessageBox::Yes)
    {
        this->close();
    }
}

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

void Widget::on_Btn1_clicked()
{
    if(ui->NamelineEdit->text()=="admin"&&ui->PassWdlineEdit->text()=="123456")
    {
        int res = QMessageBox::information(this,"information","Log in success",QMessageBox::Ok);
        if(QMessageBox::Ok==res)
        {
            this->close();
            emit my_jump();
        }
    }
    else
    {
        int res = QMessageBox::information(this,
                                 "information",
                                 "The account and password do not match,Do you want to log in again",
                                 QMessageBox::Yes|QMessageBox::No);
        if(res==QMessageBox::Yes)
        {
            ui->PassWdlineEdit->clear();
            QMessageBox::information(this,"information","continue Log in",QMessageBox::Ok);
        }
    }
}

second.cpp

#include "second.h"
#include "ui_second.h"

Second::Second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Second)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);//去掉空白部分
}

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

void Second::my_slots()
{
    this->show();
}

main.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::my_slots);

    return a.exec();
}

运行结果

QT-Day3-LMLPHPQT-Day3-LMLPHPQT-Day3-LMLPHPQT-Day3-LMLPHP

02-25 10:27