【Qt】仿照qq界面的设计

widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>//QT中信息调试类,用于输出数据,无需使用该类的实例化对象,直接使用成员函数即可
#include <QIcon>//图标类
#include <QPushButton>//按钮类
#include <QLabel>//标签类
#include <QMovie>//动画类
#include <QLineEdit>//行编辑器
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    QPushButton *btn1;//登录按钮
    QPushButton *btn2;//取消按钮
    QLineEdit *lin1;//账号输入框
    QLineEdit *lin2;//密码输入框
    QLabel *lab1;//账号小图标
    QLabel *lab2;//密码小图标
    QLabel *lab3;//动图框
    QLabel *lab4;//用户头像
    QMovie *movie;//动图

private slots:
    void my_slot(); //自定义登录函数

};
#endif // WIDGET_H

widget.cpp

复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置画布
    this->resize(500,400);
    this->setFixedSize(500,400);//设置固定大小不允许拉伸
    this->setStyleSheet("background-color:white;");
    //登录按钮
    btn1 = new QPushButton("登录",this);
    btn1->resize(100,30);//设置按钮大小
    btn1->move(149,300);//移动登录按钮
    btn1->setStyleSheet("background-color:skyblue;border-radius:10;");//设置样式表

    connect(this->btn1,&QPushButton::clicked,this,&Widget::my_slot);

    //取消按钮
    btn2 = new QPushButton("取消",this);
    btn2->resize(btn1->size());//设置取消按钮和登陆按钮一样大
    btn2->move(btn1->x()+btn1->width()+2,btn1->y());//移动取消按钮
    btn2->setStyleSheet("border-radius:10;border: 1px solid black;");//设置样式表
    //连接取消按到函数中,点击取消执行关闭窗口
    connect(this->btn2,&QPushButton::clicked,[&](){
        this->close();
    });

    //账号输入框
    lin1 = new QLineEdit(this);
    lin1->resize(280,35);//重新设置大小
    lin1->move(150,200);//移动位置
    //密码输入框
    lin2 = new QLineEdit(this);
    lin2->resize(lin1->size());//重新设置大小
    lin2->move(lin1->x(),lin1->y()+lin1->height()+5);//移动位置
    lin2->setPlaceholderText("密码");//设置占位文字
    lin2->setEchoMode(QLineEdit::Password);//设置回显模式
    //账号小图标
    lab1 = new QLabel(this);
    lab1->resize(35,35);//重新设置大小
    lab1->move(lin1->x()-36,lin1->y());//设置坐标
    lab1->setPixmap(QPixmap("D:\\icon\\icon\\zhanghao.png"));//设置图标
    lab1->setScaledContents(true);

    //密码小图标
    lab2 = new QLabel(this);
    lab2->resize(35,35);//重新设置大小
    lab2->move(lin2->x()-36,lin2->y());//设置坐标

    lab2->setPixmap(QPixmap("D:\\icon\\icon\\mima.png"));//设置图标
    lab2->setScaledContents(true);
    //动图框
    lab3 = new QLabel(this);
    lab3->resize(500, 160);//重新设置大小
    movie = new QMovie("D:\\icon\\icon\\0019.gif");
    lab3->setMovie(movie);//将动图放入标签中
    movie->start();//让动图开始动
    //让标签内容自适应大小
    lab3->setScaledContents(true);

    //用户头像
    lab4 = new QLabel(this);
    lab4->resize(70,70);//重新设置大小
    lab4->move(230,120);//设置坐标
    lab4->setPixmap(QPixmap("D:\\icon\\icon\\1.jpg"));//设置图标
    lab4->setStyleSheet("border-radius:10;");
    lab4->setScaledContents(true);
}

void Widget::my_slot()
{
    if(this->lin1->text() == this->lin2->text())
    {
        qDebug()<<"登陆成功";
    }else
    {
        qDebug()<<"登陆失败";
    }
}



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

效果展示

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00614 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术14 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript