QSplashScreen logic Issues
-
I am developing a Qt Application on Linux and seem to have some issues grasping the QSplashScreen class. What I'm trying to accomplish is a splash screen with status updates, such as loading in configurations, building menus, etc. How I'm going about it is in main.cpp I declare the splash screen and write the first message to it, then process some code to load in a configuration, then update the splash screen with a new message and process some more code. then I declare a mainwindow, build its contents, show it and call app.exec(); What seams to be happening is the splash screen isn't displayed until the app.exec is called and by then all the code has run which results in the splash screen being displayed for about a half second then the main window appears and the splash vanishes.
Any ideas on the logical approach towards this functionality of a Qt Program?
Thanks in advance...
-
Hi,
From the docs
@
QApplication app(argc, argv);
QPixmap pixmap(":/splash.png");
QSplashScreen splash(pixmap);
splash.show();
app.processEvents(); << this is what you might be missing
...
QMainWindow window;
window.show();
splash.finish(&window);
return app.exec();@Hope it helps
-
Hi, and thanks for your reply, I have calls to the processEvents() in the code, what appears to be happening is the splash screen is not being displayed until the app.exec is called which by then it's to late because all the previous code with splashScreen messages have already ran and completed.
End result, the code works, however the splash screen is only displayed for maybe a 1/2 second and dissapears due to splashScreen.finish(); I assume.
Further note, I am running ubuntu 13.10 which installs 5.0.2, 14.04 is due out in a few weeks which pushes the 5.2.x series maybe I'll force the update to see if by chances its a glitch that has been fixed already.
Thanks again.
-
You can also install the binary package to try with 5.2.1
-
I thought about that, but after reading the horror stories on upgrading to 5.2.x on ubuntu I chickened out.
I've done some more testing, if I comment out the code to create the main window and basically draw the splash screen and exit it works as expected messages get updated, etc, however if I comment out app.exec() then the splace screen does not display. I'm not sure if its a bug in 5.2.0, a design flaw in the splash code logic or I can't just wrap my head around it but IMO the splash screen should be able to be displayed and updated by calling app.processevents(); whether or not app.exec(); has been called or not. I'll post more when I get 5.2.x installed.
Thanks
-
Then build it ! :)
More seriously, it should indeed work calling only processEvents().
Do you have the same behavior with documentation's example ?
-
Here is a sample of the code I'm using...
From main.cpp
@
#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include "testsplashscreen.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);testSplashScreen *splashScreen = new testSplashScreen(); splashScreen->setPixmap(QPixmap(":/loading.gif")); splashScreen->setTextArea(QRect(210, 145, 400, 57)); splashScreen->show(); QFont splashFont; splashFont.setFamily("Ariel"); splashFont.setPixelSize(10); splashScreen->setFont(splashFont); qApp->processEvents(); splashScreen->setTextMessage(QObject::tr("Initializing....."), Qt::white); // Load Configuration files here... splashScreen->setTextMessage(QObject::tr("Building Menu...."), Qt::white); // build gui qApp->processEvents(); MainWindow w; w.show(); splashScreen->finish(&w); return a.exec();
}
@From mainwindow.cpp
@
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}MainWindow::~MainWindow()
{}
@From testsplashscreen.cpp
@
#include "testsplashscreen.h"
#include <QPainter>testSplashScreen::testSplashScreen(QWidget *parent) : QSplashScreen(parent)
{
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
}void testSplashScreen::setTextArea(QRect Area, int Alignment)
{
splashRect = Area;
Alignment = Alignment;
}void testSplashScreen::setTextMessage(QString message, QColor color)
{
splashMessage = message;
splashColor = color;
showMessage(splashMessage, splashAlignment, splashColor);
}void testSplashScreen::drawContents(QPainter *painter)
{
painter->setPen(splashColor);
painter->drawText(splashRect, splashAlignment, splashMessage);
}
@From testsplashscreen.h
@
#include "testsplashscreen.h"
#include <QPainter>testSplashScreen::testSplashScreen(QWidget *parent) : QSplashScreen(parent)
{
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
}void testSplashScreen::setTextArea(QRect Area, int Alignment)
{
splashRect = Area;
Alignment = Alignment;
}void testSplashScreen::setTextMessage(QString message, QColor color)
{
splashMessage = message;
splashColor = color;
showMessage(splashMessage, splashAlignment, splashColor);
}void testSplashScreen::drawContents(QPainter *painter)
{
painter->setPen(splashColor);
painter->drawText(splashRect, splashAlignment, splashMessage);
}
@From mainwindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);
~MainWindow();
};#endif // MAINWINDOW_H
@From splashScreen.pro
@
#-------------------------------------------------Project created by QtCreator 2014-02-15T13:52:27
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = splashScreen
TEMPLATE = appSOURCES += main.cpp
mainwindow.cpp
testsplashscreen.cppHEADERS += mainwindow.h
testsplashscreen.hRESOURCES +=
testSplashScreen.qrc
@This code will demonstrate the issues I'm having, if the splash screen is even displayed its for just a flash less than 1/2 second. The problem is that all the processing is completed before the screen will show. If you comment out the a.exec(); then absoulutely nothing is displayed at all which indicates that the QSplashScreen does not perform until the apps exec function is called.
Thanks for the help.