【QT-lineEidte动画效果

QT-lineEidte动画效果

一、演示效果

二、核心代码

c 复制代码
#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
cpp 复制代码
#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

相关推荐
qingy_20464 分钟前
【JavaWeb】JavaWeb入门之XML详解
数据库·oracle
远望清一色5 分钟前
基于MATLAB边缘检测博文
开发语言·算法·matlab
大数据面试宝典8 分钟前
用AI来写SQL:让ChatGPT成为你的数据库助手
数据库·人工智能·chatgpt
努力的小雨13 分钟前
快速上手 KSQL:轻松与数据库交互的利器
数据库·经验分享
何曾参静谧13 分钟前
「Py」Python基础篇 之 Python都可以做哪些自动化?
开发语言·python·自动化
Gentle58615 分钟前
labview中连接sql server数据库查询语句
数据库·labview
Gentle58616 分钟前
labview用sql server数据库存取数据到一个单元格
数据库·labview
Prejudices17 分钟前
C++如何调用Python脚本
开发语言·c++·python
2401_8576363919 分钟前
共享汽车管理新纪元:SpringBoot框架应用
数据库·spring boot·汽车
菲兹园长19 分钟前
表的设计(MYSQL)
数据库·mysql