设计一个登录界面窗口
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(500,500); // 设定窗口的固定大小
this->setWindowTitle("许文飞大宝贝"); //设置窗口的名字
this->setWindowIcon(QIcon(":/icon/mao.jpg")); //设置窗口的图标
this->la1 = new QLabel(this); //设置一个标签,并给定父组件
this->la1->setFixedSize(500,500); //给定标签的大小
this->la1->setPixmap(QPixmap(":/icon/dog.jpg")); //给标签填充图片
this->la1->setScaledContents(true); //图片自适应标签大小
this->la2 = new QLabel(this); //设置一个标签,并给定父组件
this->la2->setFixedSize(30,30); //给定标签的大小
this->la2->setPixmap(QPixmap(":/icon/userName.jpg")); //给标签填充图片
this->la2->setScaledContents(true); //图片自适应标签大小
this->la2->move(100,200); //移动标签
this->la3 = new QLabel(this); //设置一个标签,并给定父组件
this->la3->setFixedSize(30,30); //给定标签的大小
this->la3->setPixmap(QPixmap(":/icon/passwd.jpg")); //给标签填充图片
this->la3->setScaledContents(true); //图片自适应标签大小
this->la3->move(100,250); //移动标签
this->l1 = new QLineEdit(this); //创建一个文本编辑器 并给定父组件 绑定在此页面中
this->l1->setPlaceholderText("用户名/手机号/邮箱"); //文本编译器占位格式
this->l1->resize(250,30); //设置文本编译器的宽度和高度
this->l1->move(150,200); //移动文本编译器
this->l2 = new QLineEdit(this); //创建一个文本编辑器 并给定父组件 绑定在此页面中
this->l2->setPlaceholderText("密码"); //文本编译器占位格式
this->l2->setEchoMode(QLineEdit::Password); //密码设置成密文模式
this->l2->resize(250,30); //设置文本编译器的宽度和高度
this->l2->move(150,250); //移动文本编译器
this->b1 = new QPushButton("登录",this); //创建一个按钮, 给定父组件和数据
this->b1->move(100,350); //移动按钮
this->b1->resize(80,30); //设定按钮的大小
this->b1->setIcon(QIcon(":/icon/login.png")); // 填充图片
this->b2 = new QPushButton("取消",this); //创建一个按钮, 给定父组件和数据
this->b2->move(350,350); //移动按钮
this->b2->resize(80,30); //设定按钮的大小
this->b2->setIcon(QIcon(":/icon/cancel.png")); // 填充图片
connect(this->b1, &QPushButton::clicked, this, &Widget::login_slots);//QT5版本连接 连接b1按钮和login——slots函数
connect(this->b2, &QPushButton::clicked, [&](){
this->close();
}); //手动连接信号与槽,不需要接收者,直接执行循环逻辑即可,此时可以使用Lmabda表达式
}
Widget::~Widget()
{
delete ui;
}
void Widget::login_slots()
{
if (QString("1234") == l1->text() && QString("1234") == l2->text())
{
qDebug()<<"登录成功";
this->close();
}else
{
qDebug()<<"密码错误";
this->l2->clear();
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
signals:
void my_signal();
public slots:
void login_slots();
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QPushButton* b1;
QPushButton* b2;
QLabel* la1;
QLabel* la2;
QLabel* la3;
QLineEdit* l1;
QLineEdit* l2;
};
#endif // WIDGET_H