2 Star 1 Fork 0

朱洪君/QT-MediaPlayer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lyriclabel.cpp 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
朱洪君 提交于 2023-12-29 10:57 . base func and test
#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());
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhu_hong_jun/qt-media-player.git
git@gitee.com:zhu_hong_jun/qt-media-player.git
zhu_hong_jun
qt-media-player
QT-MediaPlayer
master

搜索帮助