Qt作业2

发布于:2024-05-08 ⋅ 阅读:(21) ⋅ 点赞:(0)

1、思维导图

2、练习:优化登录框,输入完用户名和密码后,点击登录,判断账户是否为 Admin 密码 为123456,如果判断成功,则输出登录成功,并关闭整个登录界面,如果登录失败,则提示登录失败,并将账号和密码的行编辑器中的内容清空,账号密码要大于等于五位。

源文件:

#ifndef MYWIDGET_H
#define MYWIDGET_H


#include <QWidget>
#include<QIcon> //图标类
#include<QLabel> //标签类
#include<QMovie> //动图类
#include<QLineEdit> //行编辑器类
#include<QPushButton> //按钮类
#include <QDebug> //信息调试类

class MyWidget : public QWidget
{
    Q_OBJECT
    QPushButton *btn;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
    QMovie *mv;
public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
public slots:
    void btn_clicked();
    void edit_changed();
};

#endif // MYWIDGET_H

头文件:

#include "mywidget.h"


MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(540,415);
    this->setFixedSize(540,415);
    this->setWindowTitle("盗版QQ");
    this->setWindowIcon(QIcon("D:\\yans\\color\\pictrue\\qq.png"));
    this->setStyleSheet("background-color:white");
    this->setWindowFlag(Qt::FramelessWindowHint);

    this->lab1 = new QLabel(this);
    lab1->resize(540, 160);
    lab1->setStyleSheet("background-color:pink");
    this->mv = new QMovie("D:\\yans\\color\\pictrue\\zz.gif");
    lab1->setMovie(mv);
    mv->start();
    lab1->setScaledContents(true);

    this->lab2 = new QLabel(this);
    lab2->resize(30,30);
    lab2->move(120,210);
    lab2->setPixmap(QPixmap("D:\\yans\\color\\pictrue\\wodepeizhenshi.png"));
    lab2->setScaledContents(true);

    this->lab3 = new QLabel(this);
    lab3->resize(30,30);
    lab3->move(120, 260);
    lab3->setPixmap(QPixmap("D:\\yans\\color\\pictrue\\passwd.jpg"));
    lab3->setScaledContents(true);

    this->edit1 = new QLineEdit(this);
    edit1->resize(275,30);
    edit1->move(155,210);
    edit1->setPlaceholderText("QQ号/手机号/邮箱");

    this->edit2 = new QLineEdit(this);
    edit2->resize(275,30);
    edit2->move(155,260);
    edit2->setPlaceholderText("密码");
    edit2->setEchoMode(QLineEdit::Password);

    this->btn = new QPushButton("登录",this);
    btn->resize(300,45);
    btn->move(120,345);
    //样式函数setStyleSheet()
    btn->setStyleSheet("background-color:yellow;border-radius:5px;color:white");

    QObject::connect(btn,&QPushButton::clicked,this,&MyWidget::btn_clicked);
    QObject::connect(btn,&QPushButton::clicked,this,&MyWidget::edit_changed);
}

MyWidget::~MyWidget()
{

}
void MyWidget::btn_clicked()
{
    if(this->edit1->text()=="123456"&&this->edit2->text()=="123456")
    {
    qDebug()<<"登录成功";
    this->close();
    }
    else {
    qDebug()<<"登录失败";
    this->edit1->clear();
    this->edit2->clear();
    }
}
void MyWidget::edit_changed()
{
    if(this->edit1->text().length()>=5&&this->edit2->text().length()>=5)
    {
        this->btn->setStyleSheet("background-color:blue");
    }
    else
    {
        this->btn->setStyleSheet("background-color:yellow");
    }
}