How to display a splash screen in Qt ?

Page  
2

May 8, 2012

KA51O KA51O
Robot Herder
478 posts

I wonder why you think the code I posted only works on Ubuntu? I wrote this code using visual studio running on a Windows Xp system. I haven’t tested it on Linux, but it should run on both platforms.

May 8, 2012

entricular entricular
Lab Rat
11 posts

Hello KA51O,

You need to add a Qt resource file. In order for your code to display on Windows Vista, 7.

This is your code and I included what the Qt Resource file should look like and then you can run. Hopefully, your system is configured to run Qt commands from the command line.

Create a directory and pack all your source code and files into this directory
The files would consist of main.cpp, splash.png, data.qrc file. Then run the following. Without this data.qrc resource file your splash screen image did not display on my Windows Vista/7 system. However, it did run on Ubuntu Linux without the data.qrc file.

qmake -project
qmake
make

This is your code I included the #include directives and added the Qt Resource file known as data.qrc

What your code does is display a full page splash screen for 3 seconds. You may want to minimize the full screen splashscreen and have it display for 5 seconds. In my opinion your code needs to be tweaked.

main.cpp

  1. #include <QtCore>
  2. #include <QApplication>
  3. #include <QPixmap>
  4. #include <QSplashScreen>
  5. #include <QWidget>
  6. #include <QMainWindow>
  7. #include <QTimer>
  8. #include <QThread>
  9. #include <QDesktopWidget>
  10. #include <QPainter>
  11.  
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15.  QApplication app(argc, argv);
  16.  QMainWindow* viewer = new QMainWindow();
  17.  QImage splashScrImage ("splash.png");
  18.  QSize screenSize = QApplication::desktop()->geometry().size();
  19.  QImage splashScr (screenSize, QImage::Format_ARGB32_Premultiplied);
  20.  QPainter painter (&splashScr);
  21.  painter.fillRect(splashScr.rect(), Qt::black);
  22.  QImage scaled = splashScrImage.scaled(screenSize, Qt::KeepAspectRatio);
  23.  QRect scaledRect = scaled.rect();
  24.  scaledRect.moveCenter(splashScr.rect().center());
  25.  painter.drawImage(scaledRect, scaled);
  26.  QPixmap Logo;
  27.  Logo.convertFromImage(splashScr);
  28.  QSplashScreen splashScrWindow (viewer, Logo, Qt::WindowStaysOnTopHint);
  29.  splashScrWindow.move(0,0);
  30.  splashScrWindow.show();
  31.  QTimer::singleShot(3000, &splashScrWindow ,SLOT(close()));
  32.  viewer->show();
  33.  return app.exec();
  34. }

data.qrc

  1. <RCC>
  2.     <qresource prefix="/">
  3.         <file>splash.png</file>
  4.     </qresource>
  5. </RCC>

May 8, 2012

KA51O KA51O
Robot Herder
478 posts

It was just a code snippet and not supposed to run out of the box. You may be suprised to find out that you’re the only one who has the expectation to receive fully compilable code in forum threads.

BTW if the file “splash.png” is located in the same folder as the executable, the code will work without a resource file on any platform.

Anyways thanks for the effort.

May 12, 2012

entricular entricular
Lab Rat
11 posts

Hello, KA51O and MuldeR

This is a link to the splash page code I was modifying, here you will find a sample application created using the display splash page method. See if you can compile this and run it on your computer and tell me what you think about it.

http://entricularresearchprojects.comyr.com/myprojects.htm

Page  
2

  ‹‹ How does qHash(QStringRef&) work?      Combine floating QDockWidgets and into a single floating window with tabs? ››

You must log in to post a reply. Not a member yet? Register here!