Qt 初始屏幕未显示

Qt Splash Screen not showing

本文关键字:显示 屏幕 Qt      更新时间:2023-10-16

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

主.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();
}

闪光窗口.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

命令以这样的方式运行,即在运行之前不会显示初始屏幕,不显示,我找不到修复方法。

[编辑] 与闭包对应的代码部分放错了位置,尽管正确放置后仍然不起作用。

您至少有两个问题正在进行中:

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

void QCoreApplication::p rocessEvents(QEventLoop::P rocessEventsFlags flags = QEventLoop::AllEvents) [static]

  • 我建议根据文档在第一个中使用QSplashScreen。请注意示例中对处理事件的显式调用。下面的代码对我来说效果很好。

主.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.6以来不是注释掉setWindowFlags语句的地方。