ユーザーがメイン ウィンドウを待機している間に小さなアニメーション (アニメーション GIF) を表示する単純なスプラッシュ スクリーン拡張機能があります。問題は、すべてではなく最初のフレームのみが表示されることです。
class SplashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit SplashScreen(const QPixmap& pixmap, const QString& animation, Qt::WindowFlags flags = 0);
protected:
void paintEvent(QPaintEvent* event);
signals:
void frameChanged();
private slots:
void convertFrameChanged(int)
{
repaint();
}
private:
QMovie movie;
};
SplashScreen::SplashScreen(const QPixmap& pixmap, const QString& animation, Qt::WindowFlags flags)
: QSplashScreen(pixmap, flags),
movie(animation)
{
movie.start();
connect(&(movie), SIGNAL(frameChanged(int)), this, SLOT(convertFrameChanged(int)));
}
void SplashScreen::paintEvent(QPaintEvent* event)
{
QSplashScreen::paintEvent(event);
QPixmap frame = movie.currentPixmap();
QRect rect = frame.rect();
rect.moveCenter(this->rect().center());
if (rect.intersects(event->rect()))
{
QPainter painter(this);
painter.drawPixmap(rect.left(), rect.top(), frame);
}
}
編集:
SplashScreen コンストラクターで QTimer を使用して再描画を呼び出そうとしました:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(doRefresh()));
timer->start(1000);
スプラッシュスクリーンにスロットを追加:
void doRefresh()
{
repaint();
}
しかし、これもうまくいきませんでした。doRefresh は呼び出されません。QTimer には、既存のイベント ループも必要なようです。