【QT-lineEidte动画效果

news/发布时间2024/5/15 1:03:29

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.bcls.cn/GHrs/3195.shtml

如若内容造成侵权/违法违规/事实不符,请联系编程老四网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

构造器详解

定义: 是一种特殊类型的方法&#xff0c;用于创建对象时初始化对象的状态。 使用new关键字创建对象 构造器特点: 1.和类名相同 2.没有返回值 public class Person {String name;public Person() {this.name"John";}}public class Test {public static void main…

解决docker中运行的jar包连不上前端程序

目录 检查端口映射 查看容器的 IP 地址 检查容器网络设置 防火墙和网络策略 前端程序配置 跨域资源共享 (CORS) 日志查看 连接问题通常涉及到网络配置和端口映射。确保你在 Docker 中运行的 JAR 包可以被前端程序访问&#xff0c;可以采取以下步骤来解决问题&#xff1a…

美团面试:说说Java OOM的三大场景和解决方案?

美团面试&#xff1a;说说Java OOM的场景和解决方案&#xff1f; 尼恩说在前面 在40岁老架构师 尼恩的读者交流群(50)中&#xff0c;最近有小伙伴拿到了一线互联网企业如得物、阿里、滴滴、极兔、有赞、希音、百度、网易、美团的面试资格&#xff0c;遇到很多很重要的面试题&…

C语言---二维数组指针

1.int a[3][4] {0}; ---------------------------------------- printf("%d\n",sizeof(a)); 12元素&#xff0c;12*448字节&#xff1b; ------------------------------------- printf("%d\n",sizeof(a[0][0])); 一个元素&#xff0c;4字节&#xff1…

【springblade】springblade(bladeX) 数据权限失效原因分析

文章目录 数据权限接口权限 前言&#xff1a;最近博主在按照bladeX官方文档 配置数据权限 结果发现失效了&#xff0c;网上搜了一下没找到合适的答案&#xff0c;本着求人不如求己的精神&#xff0c;自己调试了一下发现了问题所在&#xff0c;也大致看了一下bladeX的权限逻辑。…

【EI会议征稿通知】第十届能源材料与环境工程国际学术会议(ICEMEE 2024)

第十届能源材料与环境工程国际学术会议&#xff08;ICEMEE 2024&#xff09; 2024 10th International Conference on Energy Materials and Environment Engineering 随着前9年的成功&#xff0c;ICEMEE在2024年迎来了第10届。很荣幸地宣布&#xff0c;第十届能源材料与环境…

算法刷题:找到字符串中所有的字母异位词

找到字符串中所有的字母异位词 .题目链接题目详情题目解析算法原理滑动窗口流程图定义指针及变量进窗口判断出窗口更新结果 我的答案 . 题目链接 找到字符串中所有的字母异位词 题目详情 题目解析 所谓的异位词,就是一个单词中的字母,打乱顺序,重新排列得到的单词 如:abc-&g…

小清新卡通人物404错误页面源码

小清新卡通人物404错误页面源码由HTMLCSSJS组成,记事本打开源码文件可以进行内容文字之类的修改&#xff0c;双击html文件可以本地运行效果&#xff0c;也可以上传到服务器里面&#xff0c;重定向这个界面 蓝奏云&#xff1a;https://wfr.lanzout.com/i6XbU1olftde

SQL Developer 小贴士:显示RAC配置

前提&#xff1a; 已建立2节点RAC已在SQL Developer中建立了2个连接&#xff0c;分别到RAC的两个节点 然后单击菜单View>DBA&#xff0c;分别连接RAC节点1和节点2&#xff0c;并组织成目录&#xff08;不必须&#xff0c;但建议&#xff09;。 在两处可以体现为RAC配置。第…

strings.xml补充知识

复数名词 <plurals name"book"><item name"one">book</item><item name"others">books</item> </plurals>int bookCount 4; Resources res getResources(); String bookCount res.getQuantityString(R.…

【开源】JAVA+Vue.js实现医院门诊预约挂号系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 功能性需求2.1.1 数据中心模块2.1.2 科室医生档案模块2.1.3 预约挂号模块2.1.4 医院时政模块 2.2 可行性分析2.2.1 可靠性2.2.2 易用性2.2.3 维护性 三、数据库设计3.1 用户表3.2 科室档案表3.3 医生档案表3.4 医生放号…

腾讯云4核8G12M服务器支持多少人在线?

4核8G服务器支持多少人同时在线访问&#xff1f;阿腾云的4核8G服务器可以支持20个访客同时访问&#xff0c;关于4核8G服务器承载量并发数qps计算测评&#xff0c;云服务器上运行程序效率不同支持人数在线人数不同&#xff0c;公网带宽也是影响4核8G服务器并发数的一大因素&…

NDK的log.h使用__android_log_print报错app:buildCMakeDebug[x86_64]

org.gradle.api.tasks.TaskExecutionException: Execution failed for task :app:buildCMakeDebug[x86_64] 重点是 Execution failed for task :app:buildCMakeDebug[x86_64]. 我的代码&#xff1a; #include <android/log.h> #define LOG_TAG "MyJNI" #d…

MySQL之json数据操作

1 MySQL之JSON数据 总所周知&#xff0c;mysql5.7以上提供了一种新的字段格式json&#xff0c;大概是mysql想把非关系型和关系型数据库一口通吃&#xff0c;所以推出了这种非常好用的格式&#xff0c;这样&#xff0c;我们的很多基于mongoDB的业务都可以用mysql去实现了。当然…

关于Android下gralloc,hwcompoer以及surface模块的重新认识

关于Android下gralloc&#xff0c;hwcompoer以及surface模块的重新认识 引言 欠债还钱天经地义&#xff0c;知识的债也是如此&#xff01;这不必须得将我前面欠下来的债给补上&#xff01;对于任何复杂的知识点&#xff0c;我们都可以采用庖丁解牛的学习方式&#xff0c;一步步…

贪心算法练习day2

删除字符 1.题目及要求 2.解题思路 1&#xff09;初始化最小字母为‘Z’&#xff0c;确保任何字母都能与之比较 2&#xff09;遍历单词&#xff0c;找到当前未删除字母中的最小字母 3&#xff09;获取当前位置的字母 current word.charAt(i)&#xff1b; 4&#xff09;删…

【MATLAB源码-第144期】基于matlab的蝴蝶优化算法(BOA)无人机三维路径规划,输出做短路径图和适应度曲线。

操作环境&#xff1a; MATLAB 2022a 1、算法描述 ​蝴蝶优化算法&#xff08;Butterfly Optimization Algorithm, BOA&#xff09;是基于蝴蝶觅食行为的一种新颖的群体智能算法。它通过模拟蝴蝶个体在寻找食物过程中的嗅觉导向行为以及随机飞行行为&#xff0c;来探索解空间…

安卓游戏开发之音频技术优劣分析

一、引言 在安卓游戏开发中&#xff0c;音频处理技术扮演着至关重要的角色&#xff0c;它不仅能够增强游戏的沉浸感和玩家体验&#xff0c;还能通过声音效果传达关键的游戏信息。以下将对几种常见的安卓游戏音频处理技术进行优劣分析&#xff0c;并结合应用场景来阐述其特点。 …

Docker之查看并获取最新Ubuntu镜像(十)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

跳槽前应该做好哪些准备?

第一次求职也好&#xff0c;还是换工作也罢&#xff0c;都需要有严谨的考虑。对于已经工作上班的朋友来说&#xff0c;切不可轻易地辞掉工作&#xff0c;想要跳槽&#xff0c;一定要三思而后行&#xff0c;有一个周密的部署。跳槽有好处&#xff0c;也有弊端&#xff0c;频繁的…
推荐文章