How to display a splash screen in Qt ?
Page |
1 |
Is this the correct way to display a splash screen in Qt ? This code was tested and confirmed( it works ) on Ubuntu Linux 10.04 LTS with Qt 4.7.4 SDK installed. I tested the code with Windows Vista and Qt 4.7.4 and it did not work.
Make sure you have the splashpage.png file in the directory. Use the GIMP http://www.gimp.org to create a simple splashpage image of 600 × 400. When you reply to this question please do not half or snippet your code please reply with the full source code and make sure you test you code before you post it. Snippets and half code is very frustrating to a newbie coder.
Instructions to run this code:
1. Create a directory, such as SplashPageApplication
2. Change into the directory
3. Create a file named main.cpp in your newly created directory, type/copy/paste the code below in the file save as main.cpp
4. Run qmake -project, qmake, make
5. Then run application
- #include <QtCore>
- #include <QApplication>
- #include <QPixmap>
- #include <QSplashScreen>
- #include <QWidget>
- #include <QMainWindow>
- #include <QTimer>
- #include <QThread>
- {
- public:
- static void sleep(unsigned long secs) {
- }
- };
- int main(int argc, char *argv[])
- {
- splash.show();
- qApp->processEvents();//This is used to accept a click on the screen so that user can cancel the screen
- QMainWindow mainWin;
- mainWin.setWindowTitle("Qt Application"); //Set the title of your main Qt Application
- mainWin.resize(600, 500);
- I::sleep(5); // Splash page is shown for 5 seconds
- mainWin.show(); //This will maximize to fullscreen the size of your application window after the splash page displays
- splash.finish(&mainWin);
- splash.raise();
- return app.exec();
- }
[Edit: Added @ tags; mlong]
19 replies
Well, you create the Splash Screen and you show() it. But you are NOT running an event loop until the app.exec() at the very end. Your qApp->processEvents() probably is sufficient that the Splash Screen gets drawn once, but it certainly won’t be “responsive” to user actions. processEvents() processes all pending events, yes. But it won’t block and run a “loop” to wait for upcoming messages. Instead it will simply return as soon as possible!
I would use something like this and do all the required “initialization” work in a separate thread:
- int main(int argc, char *argv[])
- {
- if(pixmap.isNull())
- {
- }
- splash.setEnabled(false); //Prevent user from closing the splash
- splash.show();
- app.processEvents(); //Make sure splash screen gets drawn ASAP
- QEventLoop loop;
- MyInitThread *thread = new MyInitThread();
- thread->start();
- loop.exec(); //Do event processing until the thread has finished!
- splash.hide();
- QMainWindow mainWin;
- mainWin.setWindowTitle("My Example Application");
- mainWin.resize(640, 480);
- mainWin.show();
- return app.exec();
- }
@MuldeR
For the above code are these two or three separate files such as main.cpp, mainwindow.cpp, mainwindow.h ? Can you please tell me what you are naming your files. I rather have the full source code that you are submitting then snippets of code. I am having problems compiling your code using qmake -project, qmake, make.
Did you test this code ?
Does it work ?
Hey MuldeR
I tried modifying/hacking the initial code to include your suggestions and I usually get a bunch of error messages such as the following:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:32: error: ‘MyInitThread’ was not declared in this scope
main.cpp:32: error: ‘thread’ was not declared in this scope
main.cpp:32: error: expected type-specifier before ‘MyInitThread’
main.cpp:32: error: expected ‘;’ before ‘MyInitThread’
main.cpp:33: error: ‘connect’ was not declared in this scope
main.cpp:13: error: ‘static void I::sleep(long unsigned int)’ is protected
main.cpp:41: error: within this context
make: *** [main.o] Error 1
Do you think you can type/copy/paste this code with improvements or your suggestions that work ?
See the following code:
- #include <QtCore>
- #include <QApplication>
- #include <QPixmap>
- #include <QSplashScreen>
- #include <QWidget>
- #include <QMainWindow>
- #include <QTimer>
- #include <QThread>
- {
- protected:
- static void sleep(unsigned long secs) {
- }
- };
- int main(int argc, char *argv[])
- {
- splash.show();
- app.processEvents(); //Make sure splash screen gets drawn ASAP
- QEventLoop loop;
- MyInitThread *thread = new MyInitThread();
- connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
- thread->start();
- loop.exec(); //Do event processing until the thread has finished!
- QMainWindow mainWin;
- mainWin.setWindowTitle("Qt Application"); //Set the title of your main Qt Application
- mainWin.resize(600, 500);
- I::sleep(5); // Splash page is shown for 5 seconds
- mainWin.show(); //This will maximize to fullscreen the size of your application window after the splash page displays
- splash.finish(&mainWin);
- splash.raise();
- return app.exec();
- }
“MyInitThread” was just an example/suggestion on how you could call your own thread class.
Apparently you tried to instantiate a MyInitThread object, but didn’t declare a class of that name ;)
You have a QThread-derived class “I” in your code, but why did you declare a sleep() method an call it directly?
That’s pretty much identical to calling Sleep(secs) in your main thread – it will block the main thread!
If you want to do something in a separate thread, then you’ll have to do this in the QThread’s run() method.
You can launch the new thread by calling QThread::start(). See my code above…
Okay this is your code, that I will post below please fix your code so it compiles without errors. Is it possible for you to do that ?
All I did was add the #include directives and move the second half of the code to the beginning.
I would really like to see the full source code of your example/suggestion which works.
All you have to do is copy/paste the corrected code in the box it’s not too hard to do is it ?
- #include <QtCore>
- #include <QApplication>
- #include <QPixmap>
- #include <QSplashScreen>
- #include <QWidget>
- #include <QMainWindow>
- #include <QTimer>
- #include <QThread>
- {
- protected:
- void run(void)
- {
- /* Do whatever needs to be done to init your application! */
- }
- };
- int main(int argc, char *argv[])
- {
- splash.show();
- app.processEvents(); //Make sure splash screen gets drawn ASAP
- QEventLoop loop;
- MyInitThread *thread = new MyInitThread();
- connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
- thread->start();
- loop.exec(); //Do event processing until the thread has finished!
- QMainWindow mainWin;
- splash.hide();
- mainWin.show();
- return app.exec();
- }
I think it should be straight forward to get the basic idea from the code I posted here:
http://qt-project.org/forums/viewreply/84757/
If you have a specific question, feel free to ask. But you can’t expect that somebody else will write the complete code for you. Instead you better learn how to do it yourself. But as it seems you have more problems with C++ in general than with Qt in particular, I would suggest to start with a basic C++ book/tutorial though…
This code [qt-project.org] I posted a while ago worked for me. Maybe its helpful for you.
Hey KA510, I looked at the code you posted and although it is interesting it is not exactly what I am looking for. I noticed that this code you posted works only on Ubuntu Linux and I was looking for code that worked on both Linux and Windows. So far the closest I have come to what I am looking for is the following code below.
main.cpp
- #include <QtGui/QApplication>
- #include "splashscreen.h"
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- MainWindow w;
- // lock orientation to portrait when showing the splash screen
- w.lockPortraitOrientation();
- // SplashScreen is derived from QSplashScreen that blocks mouse
- // press events, prevents user from clicking on splashscreen to
- // hide it.
- // connect loader's progress notifications to splashscreen
- // on loader done signal, close splashscreen and show mainwindow
- #if defined(Q_WS_S60)
- #else
- #endif
- splash.show();
- splash.raise();
- return a.exec();
- }
mainwindow.cpp
- #include "mainwindow.h"
- #include <QtGui/QApplication>
- #include <QMenuBar>
- #include <QWidgetList>
- // needed for S60-specific orientation/softkey handling
- #ifdef Q_WS_S60
- #include <coemain.h>
- #include <aknappui.h>
- #endif
- m_progress(0)
- {
- m_states.insert(15, "Loading resources");
- m_states.insert(30, "Validating");
- m_states.insert(50, "Loading plugins");
- m_states.insert(66, "Initializating");
- m_states.insert(100, "Preparing to launch");
- connect(&m_timer, SIGNAL(timeout()), this, SLOT(doSomething()));
- m_timer.start(500);
- }
- // simulates some activity and fires off notification signals
- void DummyLoader::doSomething()
- {
- m_progress += (qrand() % 5) + 3;
- m_progress = qMin(100, m_progress);
- if ( m_progress == 100 ) {
- m_timer.stop();
- emit done();
- return;
- }
- if ( i != m_states.constEnd() ) {
- }
- }
- m_orientation(-1)
- {
- menuBar()->addAction(tr("Exit"), this, SLOT(close()));
- removeContextMenus();
- // a dummy progresss notifier as if we were loading something...
- m_loader = new DummyLoader(this);
- }
- void MainWindow::removeContextMenus()
- {
- // Remove context menu from the all widgets.
- foreach(w,widgets) {
- }
- }
- void MainWindow::startUpMaximized()
- {
- // release orientation lock (if set)
- releaseOrientation();
- showMaximized();
- }
- // Lock S60 app orientation to portrait - used when showing the splash screen
- void MainWindow::lockPortraitOrientation()
- {
- #ifdef Q_WS_S60
- CAknAppUi* s60AppUi = dynamic_cast<CAknAppUi*> (CCoeEnv::Static()->AppUi());
- // save the old orientation
- m_orientation = (int)s60AppUi->Orientation();
- TRAP_IGNORE(
- if (s60AppUi) {
- // Lock portrait orientation when showing the splash screen
- s60AppUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
- });
- #endif
- }
- // Releases the orientation lock (restores previous orientation)
- void MainWindow::releaseOrientation()
- {
- // do nothing if orientation is not locked
- if( m_orientation == -1 )
- return;
- #ifdef Q_WS_S60
- CAknAppUi* s60AppUi = dynamic_cast<CAknAppUi*> (CCoeEnv::Static()->AppUi());
- TRAP_IGNORE(
- if (s60AppUi) {
- s60AppUi->SetOrientationL((CAknAppUiBase::TAppUiOrientation)m_orientation);
- });
- #endif
- }
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QTimer>
- #include <QMap>
- {
- Q_OBJECT
- public:
- signals:
- void done();
- public slots:
- void doSomething();
- private:
- int m_progress;
- QTimer m_timer;
- };
- {
- Q_OBJECT
- public:
- ~MainWindow() {}
- const DummyLoader* loader() const { return m_loader; }
- public slots:
- void startUpMaximized();
- void lockPortraitOrientation();
- void releaseOrientation();
- private:
- void removeContextMenus();
- private:
- int m_orientation;
- DummyLoader* m_loader;
- };
- #endif // MAINWINDOW_H
splashscreen.h
- #ifndef SPLASHSCREEN_H
- #define SPLASHSCREEN_H
- #include <QSplashScreen>
- {
- Q_OBJECT
- public:
- protected: // reimp
- if(!m_blocking)
- }
- private:
- bool m_blocking;
- };
- #endif // SPLASHSCREEN_H
data.qrc
Also need to create a mainwindow.ui file using Qt Designer with dimensions 600×400
To run this code see the directions provided below:
Create a main directory and inside of the directory created an images directory, store your created splash.png image inside of the images directory. Then copy all 5 files into the main directory and use Qt Creator to create a mainwindow.ui file with the dimensions of 600×400. Then run qmake -project, qmake, make. This application runs on Ubuntu Linux and Windows. However, I am trying to figure out how to maximize the main application window to fit the whole computer screen. At this time all it does is show the splashscreen for a few seconds then it kicks it out to a small window/box of the main application.
It’s probably because they want to point out a specific aspect and therefore paste the relevant lines.
Posting loads of code on a forum isn’t exactly what encourages people to post a reply. If you post your complete program and ask “why it isn’t working?” it will be pretty much impossible to give an useful answer. If, instead, you only post a small excerpt from your program – the specific part where you have a problem/question – then it will be much easier for other users to understand the issue and to give the desired reply…
Qt calls its header files exactly like the class they define; shouldn’t be too hard to figure out the right include ;)
You must log in to post a reply. Not a member yet? Register here!


