我有一个简单的初始屏幕扩展,它应该在用户等待主窗口时显示一点动画(动画 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 还需要一个已经存在的事件循环。
最佳答案
假设在调用 QApplication::exec() 函数之前显示启动画面,那么很可能没有处理事件,因此您需要在 QApplication 对象上调用 processEvents。
注意帮助示例:-
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap pixmap(":/splash.png");
QSplashScreen splash(pixmap);
splash.show();
app.processEvents(); // need to process events manually
...
QMainWindow window;
window.show();
splash.finish(&window);
return app.exec();
在这种情况下,它讨论了在初始屏幕上调用 raise(),使用 QTimer 以确保它保持在顶部,但需要 processEvents 调用以使 QTimer 起作用。
正如 Qt 帮助中所述,当单击鼠标按钮时关闭闪屏:-
由于启动画面通常在事件循环开始运行之前显示,因此有必要定期调用 QApplication::processEvents() 以接收鼠标点击。
这同样适用于动画 gif。
关于c++ - 在 QSplashScreen 中显示 QMovie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18334499/