代码拉取完成,页面将自动刷新
#include "lyriclabel.h"
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QSettings>
LyricLabel::LyricLabel(QWidget *parent)
: QLabel(parent)
{
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); //window标识+无window边框+window置顶
setAttribute(Qt::WA_TranslucentBackground);
setText("欢迎使用PitchMusic桌面歌词");
setFrameShape(QFrame::NoFrame);
setMinimumSize(800, 60);
setMaximumSize(800, 60);
QSettings settings;
settings.value("lyricLabel/rect").isNull() ? setGeometry(450, 700, 800, 60) : setGeometry(settings.value("lyricLabel/rect").toRect());
font.setPointSize(30);
font.setFamily("Times New Roman");
font.setBold(true);
setFont(font);
setAlignment(Qt::AlignCenter);
//设置歌词渐变填充
lineGradient.setStart(0, 10); //去除window上部
lineGradient.setFinalStop(0, 40);
lineGradient.setColorAt(0.1, QColor(14, 179, 255));
lineGradient.setColorAt(0.5, QColor(114, 32, 255));
lineGradient.setColorAt(0.9, QColor(14, 179, 255));
//设置mask歌词渐变填充
maskLineGradient.setStart(0, 10); //去除window上部
maskLineGradient.setFinalStop(0, 40);
maskLineGradient.setColorAt(0.1, QColor(222, 54, 4));
maskLineGradient.setColorAt(0.5, QColor(255, 72, 16));
maskLineGradient.setColorAt(0.9, QColor(222, 54, 4));
//设置定时器
connect(&timer, &QTimer::timeout, this, &LyricLabel::changeMaskRect);
}
void LyricLabel::setAddWidthByTotal(int total)
{
right = 0;
add = qMax((text().size() * 30 + total / 30 - 1)/ (total / 30), 1);
// qDebug() <<tr("text().size(): %1, right: %2, add: %3, total: %4").arg(text().size()).arg(right).arg(add).arg(total);
timer.start(step);
}
void LyricLabel::changeMaskRect()
{
right += add;
right = qMin(800, right);
update();
}
void LyricLabel::stopTimer()
{
timer.stop();
}
void LyricLabel::paintEvent(QPaintEvent *event)
{
if(!timer.isActive()) right = 0;
QPainter painter(this);
painter.setFont(font);
//绘制底色,作为衬托与清晰对比
painter.setPen(QColor(0, 0, 0, 100));
painter.drawText(0, 0, 800, 60, Qt::AlignLeft, text());
//绘制渐变,突出层次
painter.setPen(QPen(lineGradient, 0));
painter.drawText(0, 0, 800, 60, Qt::AlignLeft, text());
//绘制mask,跟随播放拉长
painter.setPen(QPen(maskLineGradient, 0));
painter.drawText(0, 0, right, 60, Qt::AlignLeft, text());
}
void LyricLabel::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
offset = event->globalPosition().toPoint() - frameGeometry().topLeft();
}
void LyricLabel::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton){
setCursor(Qt::PointingHandCursor);
move(event->globalPosition().toPoint() - offset);
}
}
void LyricLabel::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu;
menu.addAction("隐藏", this, &LyricLabel::hide);
menu.exec(event->globalPos());
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。