c++ - 在 QSplashScreen 中显示 QMovie

标签 c++ qt splash-screen

我有一个简单的初始屏幕扩展,它应该在用户等待主窗口时显示一点动画(动画 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/

相关文章:

c++ - 如何为包含 std::vector 的类实现operator[]

c++ - 向现有 Qt 应用程序添加表单

c++ - TrollTech 的 Qt 教程第 11 章中令人困惑的源代码

c++ - 在 C++ 中使用 WebKit QT 的简单网页浏览器的示例代码

javascript - ionic 2/3 : control the opening of splashScreen with LocalStorage

android - 当 i18n.locale 更改时更改原生 android 上的启动图像

c++ - 在函数周围传递 char[]

c++ - 了解将调用哪个方法

c++ - 使用 Boost property_tree 更新 XML 文件

node.js - 如何替换 Electron Forge 创建的 Electron.js 包中的默认启动屏幕