【问题标题】:Qt Splash Screen not showingQt 启动画面不显示
【发布时间】:2014-01-22 13:27:18
【问题描述】:

我有这段代码,我希望它显示一个启动画面,因为它会更大,已经制作了一种计时器,因此可以看到启动画面工作。问题是我没有看到启动画面,但代码将在启动画面不出现时运行,直接将我发送到主窗口而不显示启动画面。 这是我的代码。

main.cpp

#include <iostream>

#include <QApplication>
#include <quazip/quazip.h>

#include "splashwindow.h"
#include "mainwindow.h"
#include "database.h"

int main(int argc, char *argv[])
{
    /* Define the app */
    QApplication app(argc, argv);

    /* Define the splash screen */
    SplashWindow splashW;
    /* Show the splash screen */
    splashW.show();

    /* Download the database */
    /* Define the database */
    Downloader db;
    /* Donwloading the database */
    db.doDownload();

    /* Unzip the database */
    /* Define the database */
    //Unzipper uz;
    //uz.Unzip();

    for(int i = 0; i < 1000000; i++)
    {
        cout << i << endl;
    }

    /* Close the splash screen */
    splashW.hide();
    splashW.close();

    /* Define the main screen */
    MainWindow mainW;
    /* Show the main window */
    mainW.showMaximized();

    return app.exec();
}

splashwindow.cpp

#include <iostream>
#include <QStyle>
#include <QDesktopWidget>

#include "splashwindow.h"
#include "ui_splashwindow.h"
#include "database.h"

/* Splash screen constructor */
SplashWindow::SplashWindow (QWidget *parent) :
    QMainWindow(parent), ui(new Ui::SplashWindow)
{
    ui->setupUi(this);
    /* Set window's flags as needed for a splash screen */
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::SplashScreen);
}

/* Splash screen destructor */
SplashWindow::~SplashWindow()
{
    delete ui;
}

splashwindow.h

#ifndef SPLASHWINDOW_H
#define SPLASHWINDOW_H

#include <QMainWindow>

namespace Ui {
class SplashWindow;
}

class SplashWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit SplashWindow(QWidget *parent = 0);
    ~SplashWindow();

private:
    Ui::SplashWindow *ui;
};

#endif // SPLASHWINDOW_H

命令以这样的方式运行,在它们运行之前不会出现启动画面,不显示,我找不到解决方法。

[EDIT]闭包对应的部分代码放错了地方,虽然放对了还是不行。

【问题讨论】:

  • 也许摆脱 splashW.hide(); 会有所帮助?
  • 不起作用,既不移动也不移除。
  • 能看到SplashWindow的声明吗? (可能在splashwindow.h
  • 好的,我会编辑帖子的。
  • 为什么不使用 QSplashScreen?

标签: c++ qt qtgui qmainwindow qsplashscreen


【解决方案1】:

什么对我有用, 飞溅在 Qt 的早期版本中起作用,但自 5.6 以来没有注释掉 setWindowFlags 语句。

【讨论】:

    【解决方案2】:

    您至少存在两个问题:

    • 您将主线程发送到阻塞循环中,它无法处理包括显示窗口在内的事件。这需要一些事件处理,因此您需要在 while 循环之前调用以下方法:

    void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents) [static]

    • 我建议首先按照documentation 使用QSplashScreen。请注意示例中处理事件的显式调用。下面的代码对我来说很好。

    main.cpp

    #include <QApplication>
    #include <QPixmap>
    #include <QMainWindow>
    #include <QSplashScreen>
    
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QPixmap pixmap("splash.png");
        QSplashScreen splash(pixmap);
        splash.show();
        app.processEvents();
        for (int i = 0; i < 500000; ++i)
            qDebug() << i;
        QMainWindow window;
        window.show();
        splash.finish(&window);
        return app.exec();
    }
    

    main.pro

    TEMPLATE = app
    TARGET = main
    greaterThan(QT_MAJOR_VERSION, 4):QT += widgets
    SOURCES += main.cpp
    

    【讨论】:

    • 现在时不时出现闪屏,进度条不跳动。我将继续使用该程序并稍后返回启动屏幕,尽管您给我上了很好的一课并将继续尝试这些内容。
    • 这在带有 Qt 5.5.1 的 Ubuntu 14.04 上对我不起作用我仍然在窗口出现之前打开了初始屏幕。但是像here 这样的循环(总共花费了 30 毫秒,而不是 1 秒的时间)确实有效。
    猜你喜欢
    • 2015-12-18
    • 2018-12-07
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2014-05-05
    相关资源
    最近更新 更多