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

效果展示

相关推荐
2501_944934731 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
黎雁·泠崖2 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472463 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
TechWJ3 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
lly2024063 小时前
C++ 文件和流
开发语言
m0_706653234 小时前
分布式系统安全通信
开发语言·c++·算法
寻寻觅觅☆4 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++
杨了个杨89824 小时前
memcached部署
qt·websocket·memcached
lightqjx4 小时前
【C++】unordered系列的封装
开发语言·c++·stl·unordered系列
zh_xuan4 小时前
kotlin lazy委托异常时执行流程
开发语言·kotlin