【QT-lineEidte动画效果

news2024/9/20 16:58:42

QT-lineEidte动画效果

  • 一、演示效果
  • 二、核心代码
  • 三、下载链接

一、演示效果

在这里插入图片描述

二、核心代码

#ifndef DynamicUnderlineLineEdit_H
#define DynamicUnderlineLineEdit_H

#include <QWidget>
#include <QLineEdit>
#include <QPainter>
#include <QPaintEvent>
#include <QFocusEvent>
#include <QTimer>
#include <QColor>
#include <QPointF>
#include <QPalette>

//动态下划线单行文本框:Dynamic Underline LineEdit
class DynamicUnderlineLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    explicit DynamicUnderlineLineEdit(QWidget *parent = nullptr);
    explicit DynamicUnderlineLineEdit(const QString &text, QWidget *parent = nullptr);

    void setLinePen( const QPen &focusInPen,
                    const QPen &focusOutPen = QPen(QBrush(QColor(66,66,66)),2)); // 设置线条的绘制画笔,参数:获取焦点的绘制画笔,失去焦点的绘制画笔
    void setTextColor(const QColor &textColor);                                 // 设置输入文字颜色
    void setPlaceholderTextColor(const QColor &placeholderText);                // 设置预设背景文字颜色
    void setLineSpeed(int speed);                                               // 设置线条填速度,越小越快,最小为1,实际使用可根据文本框的长度不同设置不同的速度以达到最佳观感

private:
    int right_coordinate{-1};   // 线条右侧坐标
    QTimer *timer;
    QPen inactive_pen;// 未获得焦点时线条的绘制画笔
    QPen active_pen; // 获得焦点时线条的绘制画笔

    void initializeMemberVariable();

private slots:
    void inFocus();  // 获得焦点
    void outFocus(); // 失去焦点

protected:
    virtual void paintEvent(QPaintEvent *event) override;
    virtual void focusInEvent(QFocusEvent *event) override;  // 获取焦点事件
    virtual void focusOutEvent(QFocusEvent *event) override; // 失去焦点事件
};

#endif // DynamicUnderlineLineEdit_H

#include "DynamicUnderlineLineEdit.h"

DynamicUnderlineLineEdit::DynamicUnderlineLineEdit(QWidget *parent)
    : QLineEdit{parent},
      timer{new QTimer(this)}
{
    initializeMemberVariable();
}
DynamicUnderlineLineEdit::DynamicUnderlineLineEdit(const QString &text, QWidget *parent)
    : QLineEdit::QLineEdit(text,parent)
{
    initializeMemberVariable();
}

void DynamicUnderlineLineEdit::setLinePen( const QPen &focusInPen,const QPen &focusOutPen)
{
    inactive_pen = focusOutPen;
    active_pen = focusInPen;
}

void DynamicUnderlineLineEdit::setTextColor(const QColor &textColor)
{
    const_cast<QPalette &>(palette()).setColor(QPalette::ColorRole::Text, textColor);
}

void DynamicUnderlineLineEdit::setPlaceholderTextColor(const QColor &placeholderText)
{
    const_cast<QPalette &>(palette()).setColor(QPalette::ColorRole::PlaceholderText, placeholderText);
}

void DynamicUnderlineLineEdit::setLineSpeed(int speed)
{
    timer->setInterval(speed);
}

void DynamicUnderlineLineEdit::initializeMemberVariable()
{
    setFocusPolicy(Qt::ClickFocus);
    timer->setInterval(12);
    connect(timer, &QTimer::timeout, this, &DynamicUnderlineLineEdit::inFocus);
    setAttribute(Qt::WA_TranslucentBackground);//背景透明
    setFrame(false);//无边框
    setTextMargins(10, 0, 0, 0);//设置文本左边距10像素
    inactive_pen.setColor(qRgb(66, 66, 66));
    inactive_pen.setWidth(2);
    active_pen.setColor(qRgb(0, 123, 255));
    active_pen.setWidth(2);
}

void DynamicUnderlineLineEdit::inFocus()
{
    right_coordinate += 10;
    if (right_coordinate > width())
        timer->stop();
    update();
}

void DynamicUnderlineLineEdit::outFocus()
{
    right_coordinate -= 10;
    if (right_coordinate < 0)
        timer->stop();
    update();
}

void DynamicUnderlineLineEdit::paintEvent(QPaintEvent *event)
{
    QLineEdit::paintEvent(event);
    QPainter painter = QPainter(this);
    painter.setRenderHint(QPainter::RenderHint::Antialiasing); // 抗锯齿
    painter.setPen(inactive_pen); // 设置画笔颜色和线条样式
    painter.drawLine(0, height() - inactive_pen.width(),width(), height() - inactive_pen.width()); // 在底部画未选中时的线条
    painter.setPen(active_pen);
    painter.drawLine(-2, height() - active_pen.width(),right_coordinate, height() - active_pen.width()); // 在底部画选中时的线条
}

void DynamicUnderlineLineEdit::focusInEvent(QFocusEvent *event)
{
    QLineEdit::focusInEvent(event);
    timer->disconnect();
    connect(timer, &QTimer::timeout, this, &DynamicUnderlineLineEdit::inFocus);
    timer->start();
}

void DynamicUnderlineLineEdit::focusOutEvent(QFocusEvent *event)
{
    QLineEdit::focusOutEvent(event);
    timer->disconnect();
    connect(timer, &QTimer::timeout, this, &DynamicUnderlineLineEdit::outFocus);
    timer->start();
}

三、下载链接

https://download.csdn.net/download/u013083044/88864880

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1465383.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

产品渲染3D效果图一张多少钱,哪个平台更有性价比?

产品渲染3D效果图的价格受到多方面因素的影响&#xff0c;包括但不限于产品类型、渲染难度以及输出尺寸等。如果效果图需要后期处理&#xff0c;还有可能增加其他费用。接下来&#xff0c;我们来了解一下产品渲染效果图的费用情况。 1.产品渲染3D效果图一张多少钱&#xff1f; …

最长的回文串

开始想的简单了&#xff0c;确实没想到奇数字母删去一个后也能用 解法&#xff1a; 桶排序 #include<iostream> #include<vector> #include<algorithm> using namespace std; #define endl \n #define int long long signed main() {int t;cin >> t…

【C++11】lambda表达式 | 函数包装器

文章目录 一、lambda表达式1. 为什么需要lambda表达式2. lambda的定义3. lambda的语法捕捉列表 4. 函数对象和lambda表达式的底层原理 二、函数包装器1. function包装器2. bind包装器用bind包装器绑定固定参数用bind包装器调整传参顺序无意义的绑定 3. bind包装器的意义 一、la…

【Spring】Spring MVC

目 录 一.什么是 Spring MVC&#xff1f;1.MVC 定义2.MVC 和 Spring MVC 的关系 二.为什么要学 Spring MVC&#xff1f;三.怎么学 Spring MVC&#xff1f;1.Spring MVC 创建和连接综上连接方式&#xff1a; 2.获取参数1.传递单个参数2.获取多个参数3.获取对象4.获取表单参数5.接…

Google谷歌通过文本、图像从而生成音频和视频的多模态学习模型:VideoPoet

VideoPoet是一种多模态学习模型&#xff0c;本身是一个大型语言模型&#xff08;LLM&#xff09;&#xff0c;能够理解和处理文本、图像、音频等多种信息&#xff0c;并将其融合到视频生成过程中。它不仅能够根据文字描述生成视频&#xff0c;还能给视频添加风格化效果、修复和…

8杯水要怎么喝才能防止或减轻肥胖状态?

为什么说&#xff0c;人每天要喝足8杯水呢&#xff1f;原因很多&#xff0c;皮肤干了要多喝水&#xff0c;因为皮肤缺水&#xff0c;需要滋养&#xff1b;身体底子差要多喝水&#xff0c;实际身体底子差不光要多喝水&#xff0c;还要注意饮食营养和运动锻炼&#xff1b;身体健康…

【html学习笔记】3.表单元素

1.文本框 1.1 语法 <input type "text">表示文本框。且只能写一行 1.2 属性 使用属性size 设置文本框大小 <input type"text" size"10">2. 使用属性value 来设置文本框的默认文字 <input type"text" size"…

深度学习发展里程碑事件2006-2024

2006-2024年&#xff0c;深度学习发展经历众多的里程碑事件&#xff0c;一次次地刺激着人们的神经&#xff0c;带来巨大的兴奋。电影还在继续&#xff0c;好戏在后面&#xff0c;期待…… 2006年 深度信念网络&#xff08;DBNs&#xff09;&#xff1a;Geoffrey Hinton与他的学…

maven的聚合和生命周期

什么是maven的聚合呢?就是父类直接将子类项目一起统一打包安装统一maven的生命周期 1.maven的生命周期 2.在父亲类pom文件指定需要打包的项目 实例代码: <!--maven的聚合 通过modules指定需要打包的maven项目--> <modules><module>../ithema-jopo</m…

C# OpenVINO 百度PaddleSeg实时人像抠图PP-MattingV2

目录 效果 项目 代码 下载 C# OpenVINO 百度PaddleSeg实时人像抠图PP-MattingV2 效果 项目 代码 using OpenCvSharp; using Sdcb.OpenVINO; using System; using System.Diagnostics; using System.Drawing; using System.Security.Cryptography; using System.Text; us…

基于springboot+vue的车辆管理系统(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

Java 面向对象进阶 17 接口案例(黑马)

第一种模式&#xff1a; 第二种模式&#xff1a; 推荐使用第一种&#xff0c;因为第一种的接口较少&#xff1b; 代码&#xff1a; 将Person设为abstract类的原因为&#xff1a;

c#程序,oracle使用Devart驱动解决第第三方库是us7ascii,数据乱码的问题

最近做项目&#xff0c;要跟对方系统的库进行读写&#xff0c;结果发现对方采用的是oracle的us7ascii编码&#xff0c;我们系统默认采用的是ZHS16GBK&#xff0c;导致我们客户端读取和写入对方库的数据都是乱码&#xff0c;搜索网上&#xff0c;发现需要采用独立的oracle驱动去…

电路设计(27)——交通信号灯的multisim仿真

1.功能要求 使用数字芯片设计一款交通信号灯&#xff0c;使得&#xff1a; 主干道的绿灯时间为60S&#xff0c;红灯时间为45S 次干道的红灯时间为60S&#xff0c;绿灯时间为45S 主、次干道&#xff0c;绿灯的最后5S内&#xff0c;黄灯闪烁 使用数码管显示各自的倒计时时间。 按…

Android 广播的基本概念

一.广播简介 Broadcast是安卓四大组件之一。安卓为了方便进行系统级别的消息通知&#xff0c;引入了一套广播消息机制。打个比方&#xff0c;记得原来在上课的时候&#xff0c;每个班级的教室里都会装有一个喇叭&#xff0c;这些喇叭都是接入到学校的广播室的&#xff0c;一旦…

[DP学习] 期望DP

一般思路 注&#xff1a;可以用方差求平方的期望 例题一 思路 重点&#xff1a;如何设状态&#xff0c;如何转移。 设状态 f[i] i 张能买到不同卡片的种类数的期望值&#xff08;直接对问题设置状态&#xff09; 状态转移&#xff1a;由于从f[i1]转移到 f[i] 时&#xff0…

基于springboot+vue的中小型医院网站(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

学习 python的第四天,顺便分享两首歌:we don‘ talk anymore,You ‘re Still The One

诸君晚上好&#xff0c;现在是&#x1f303;晚上&#xff0c;今天是学习python的第四个学习日&#xff0c;不知不觉学了四天了&#xff0c;还是那句话&#xff1a;不积跬步无以至千里、不积小流无以成江海&#xff01; 暂时回顾下前面的学习日吧&#xff1a; 第一个学习日----…

Python和Jupyter简介

在本notebook中&#xff0c;你将&#xff1a; 1、学习如何使用一个Jupyter notebook 2、快速学习Python语法和科学库 3、学习一些IPython特性&#xff0c;我们将在之后教程中使用。 这是什么&#xff1f; 这是只为你运行在一个个人"容器"中的一个Jupyter noteboo…

【计算机网络】数据链路层--以太网/MTU/ARP/RARP协议

文章目录 一、以太网1.以太网帧格式2.MAC地址3.局域网的转发原理 二、MTU1.什么是MTU2.MTU对IP协议的影响3.MTU对UDP影响4.MTU对于TCP协议的影响 三、ARP协议1.ARP协议的作用2.ARP数据报的格式3.ARP协议的工作流程 一、以太网 “以太网” 不是一种具体的网络, 而是一种技术标准…