效果图:
源码:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPainter> #include <QFontMetrics> #include <QPen> #include <QFont> #include <QTimer> class Widget : public QWidget { Q_OBJECT unsigned int m_FontSize; QTimer m_timer; protected slots: void OnTimeout(); protected: void paintEvent(QPaintEvent* event); public: Widget(QWidget *parent = nullptr); ~Widget(); }; #endif // WIDGET_H
#include "Widget.h"
#include <QString>
Widget::Widget(QWidget *parent) : QWidget(parent),m_timer(this)
{
m_FontSize = 0;
setFixedSize(600, 400);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(OnTimeout()));
m_timer.start(100);
}
void Widget::OnTimeout()
{
update();
}
void Widget::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QString strShow = QString("University");
QPainter painter(this);
painter.setPen(Qt::blue);
QFont font("Comic Sans MS", 5 + (m_FontSize++)%100);
painter.setFont(font);
QFontMetrics fontmetry(font);
int width = fontmetry.width(strShow);
int height = fontmetry.height();
int x = 300 - width/2;
int y = 200 - height/2;
painter.drawText(x, y, width, height, Qt::AlignCenter, strShow);
}
Widget::~Widget()
{
}