练习:优化登录框,输入完用户名和密码后,点击登录,判断账户是否为 Admin 密码 为123456,如果判断成功,则输出登录成功,并关闭整个登录界面,如果登录失败,则提示登录失败,并将账号和密码的行编辑器中的内容清空
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
,btn(new QPushButton(this)),edit1(new QLineEdit(this)),edit2(new QLineEdit(this))
{
//==============窗口相关设置=======
this->resize(540,415);
this->setFixedSize(540,415);
//窗口标题
this->setWindowTitle("盗版QQ");
//窗口图标
this->setWindowIcon(QIcon(":/pictrue/qq.gif"));
//背景颜色
this->setStyleSheet("background-color:white");
//去掉头部
this->setWindowFlag(Qt::FramelessWindowHint);
//============标签相关设置=======
QLabel *lab1 = new QLabel(this);
//设置大小
lab1->resize(540, 160);
lab1->setStyleSheet("background-color:pink");
//动图类 接收动图
QMovie *mv = new QMovie(":/pictrue/zz.gif");
//将动图放入标签中
lab1->setMovie(mv);
//让动图动起来
mv->start();
//自动适应
lab1->setScaledContents(true);
QLabel *lab2 = new QLabel(this);
lab2->resize(30,30);
lab2->move(120,210);
lab2->setPixmap(QPixmap(":/pictrue/wodepeizhenshi.png"));
lab2->setScaledContents(true);
QLabel *lab3 = new QLabel(this);
lab3->resize(30,30);
lab3->move(120, 260);
lab3->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
lab3->setScaledContents(true);
//============行编辑器相关设置=======
// QLineEdit *edit1 = new QLineEdit(this);
edit1->resize(275,30);
edit1->move(155,210);
edit1->setPlaceholderText("QQ号/手机号/邮箱");
// QLineEdit *edit2 = new QLineEdit(this);
edit2->resize(275,30);
edit2->move(155,260);
edit2->setPlaceholderText("密码");
edit2->setEchoMode(QLineEdit::Password);
//============按钮相关设置=======
// QPushButton *btn = new QPushButton("登录",this);
btn->resize(300,45);
btn->move(120,345);
//样式函数setStyleSheet()
btn->setStyleSheet("background-color:rgb(8,189,253);border-radius:5px;color:white");
// btn->setEnabled(false);
connect(edit1,&QLineEdit::textChanged,this,&MyWidget::signel);
connect(edit2,&QLineEdit::textChanged,this,&MyWidget::signel);
}
void MyWidget::signel()
{
if(edit1->text().size()>=5 && edit2->text().size()>=5){
btn->setStyleSheet("background-color:green");
}
}
MyWidget::~MyWidget()
{
}
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include<QIcon> //图标类
#include<QLabel> //标签类
#include<QMovie> //动图类
#include<QLineEdit> //行编辑器类
#include<QPushButton> //按钮类
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE
class MyWidget : public QWidget
{
Q_OBJECT
public:
QPushButton *btn;
QLineEdit *edit1;
QLineEdit *edit2;
public:
MyWidget(QWidget *parent = nullptr);
~MyWidget();
public slots:
void signel(); //自定义处理按钮4的槽函数的声明
private:
Ui::MyWidget *ui;
};
#endif // MYWIDGET_H