1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #include "mlabel.h"
- #include <QPainter>
- #include <QTimer>
- #include <QFontMetrics>
- #include <QTimerEvent>
- const QString strSpace(" ");
- MLabel::MLabel(QWidget *parent) : QLabel(parent),left(0),timerId(-1),fontSize(10)
- {
- setStyleSheet(QString("color:black;"));
- }
- void MLabel::setText(const QString & txt)
- {
- QLabel::setText(txt);
- upateLabelRollingState();
- }
- void MLabel::paintEvent(QPaintEvent *e)
- {
- QPainter p(this);
- QRect rc = rect();
- rc.setHeight(rc.height() - 2);
- rc.setWidth(rc.width() - 2);
- QFont ft = font();
- ft.setPointSize(fontSize);
- p.setFont(ft);
- p.setPen(QPen(Qt::black));
- // 设置绘制文字的开始位置,也就是将文字往左移动多少
- rc.setLeft(rc.left() - left);
- // 如果文字已经显示到末尾,则再添加一遍文字,做出循环滚动的效果
- QString strText = text();
- if (timerId >= 0) {
- strText += strSpace + text();
- }
- // 绘制文字
- p.drawText(rc, Qt::AlignVCenter, strText);
- }
- void MLabel::timerEvent(QTimerEvent *e)
- {
- if (e->timerId() == timerId && isVisible()) {
- // 每次左移1个像素
- left += 1;
- // 判断是否已经完成一遍循环,完成则恢复起始位置,重新开始循环
- QFont ft = font();
- ft.setPointSize(fontSize);
- QFontMetrics fm(ft);
- const int txtWidth = fm.width(text());
- const int spaceWidth = fm.width(strSpace);
- if ((txtWidth + spaceWidth) < left) {
- left = 0;
- }
- repaint();
- }
- QLabel::timerEvent(e);
- }
- void MLabel::resizeEvent(QResizeEvent *e)
- {
- QLabel::resizeEvent(e);
- upateLabelRollingState();
- }
- void MLabel::upateLabelRollingState()
- {
- // 获取文本大小,小于文本框长度,则无需滚动
- QFont ft = font();
- ft.setPointSize(fontSize);
- QFontMetrics fm(ft);
- int nW = fm.width(text());
- left = 0;
- // 开启文本框滚动
- if (nW > width()) {
- timerId = startTimer(100);
- }
- // 关闭文本框滚动
- else {
- if (timerId >= 0) {
- killTimer(timerId);
- timerId = -1;
- }
- }
- }
|