一.效果
二.实现
roller.h
#ifndef ROLLER_H
#define ROLLER_H
#include <QWidget>
#include <QPaintEvent>
#include <QShowEvent>
#include <QHideEvent>
#include <QTimer>
class Roller : public QWidget
{
public:
explicit Roller(QWidget *parent = nullptr);
void setText(const QString &text);
protected:
QSize sizeHint() const override;
void paintEvent(QPaintEvent* event) override;
void showEvent(QShowEvent* event) override;
void hideEvent(QHideEvent* event) override;
private:
QString m_text;
int m_offset;
QTimer m_timer;
};
#endif // ROLLER_H
roller.cpp
#include "roller.h"
#include <QPainter>
#include <QFontMetrics>
Roller::Roller(QW