一、演示效果
二、核心代码
#ifndef DynamicUnderlineLineEdit_H
#define DynamicUnderlineLineEdit_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
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);
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
#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);
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();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
三、下载链接
https://download.csdn.net/download/u013083044/88864880