【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;
}

效果展示

相关推荐
JIngJaneIL17 分钟前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码24 分钟前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web
小信啊啊39 分钟前
Go语言切片slice
开发语言·后端·golang
阿华hhh41 分钟前
Linux系统编程(标准io)
linux·开发语言·c++
南_山无梅落1 小时前
9.Python3集合(set)增删改查和推导式
java·开发语言
sg_knight1 小时前
拥抱未来:ECMAScript Modules (ESM) 深度解析
开发语言·前端·javascript·vue·ecmascript·web·esm
程序喵大人1 小时前
推荐个 C++ 练习平台
开发语言·c++·工具推荐
阿里嘎多学长2 小时前
2025-12-16 GitHub 热点项目精选
开发语言·程序员·github·代码托管
乂爻yiyao2 小时前
Java LTS版本重要升级特性对照表
java·开发语言
原来是好奇心2 小时前
深入Spring Boot源码(六):Actuator端点与监控机制深度解析
java·开发语言·源码·springboot