Qt启动画面未显示。

4
我有这段代码,我希望它能展示一个启动画面,因为它会变得更大一些。我已经做了一个计时器,所以可以看到启动画面正在工作。问题是我没有看到启动画面,但是代码在运行,而启动画面没有出现,直接跳转到主窗口而没有显示启动画面。 下面是我的代码。

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

命令的运行方式是这样的,它们在运行之前不会显示闪屏,这个问题我找不到解决方法。
[编辑] 闭包对应的代码部分被放错了位置,即使放到正确的位置后仍然无法正常工作。

无法工作,无论是移动还是删除。 - vitimiti
当然,我会编辑这篇文章。 - vitimiti
1
为什么不使用QSplashScreen? - A Person
如果你在main()函数中下载数据库,你的应用程序会看起来无响应。你可以将该代码移到你的MainWindow中,并显示一个加载或进度指示器。 - A Person
@Siidheesh:QSplashScreen也是我的想法。另外,为什么不使用QTimer而不是while循环呢? - László Papp
显示剩余2条评论
2个回答

4
您目前有至少两个问题:
  • 您将主线程置于阻塞循环中,无法处理事件,包括窗口的显示。这需要一些事件处理,因此您需要在 while 循环之前调用以下方法:

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

  • 我建议您首先使用 QSplashScreen,如documentation所述。请注意示例中显式调用事件处理程序。下面的代码对我来说可以正常工作。

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

现在启动画面时不时地显示,进度条也没有反弹。我会继续使用程序,并稍后回到启动画面,虽然你教给了我一个好的课程,我会继续尝试这些包含文件。 - vitimiti
这在我使用Ubuntu 14.04和Qt 5.5.1时没有生效,窗口出现前仍然会显示启动画面。但是像这里一样进行循环(总共30毫秒,而不是1秒的时间)就可以解决。 - Ruslan

0

对我而言有效的方法是,在Qt早期版本中可以使用Splash,但自5.6以来就不能使用setWindowFlags语句了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接