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

效果展示

相关推荐
Felix_One1 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端
norlan_jame9 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054969 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月9 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237179 天前
C语言-数组练习进阶
c语言·开发语言·算法