How to display a splash screen in Qt ?
-
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 x 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:
- Create a directory, such as SplashPageApplication
- Change into the directory
- Create a file named main.cpp in your newly created directory, type/copy/paste the code below in the file save as main.cpp
- Run qmake -project, qmake, make
- Then run application
@
#include <QtCore>
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>
#include <QWidget>
#include <QMainWindow>
#include <QTimer>
#include <QThread>
class I : public QThread
{
public:
static void sleep(unsigned long secs) {
QThread::sleep(secs);
}
};int main(int argc, char *argv[])
{QApplication app(argc, argv); QPixmap pixmap("splash.png"); //Insert your splash page image here QSplashScreen splash(pixmap); splash.show(); splash.showMessage(QObject::tr("Initiating your program now..."), Qt::AlignLeft | Qt::AlignTop, Qt::black); //This line represents the alignment of text, color and position 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]
-
Please use @ tags around your code.
-
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[])
{
QApplication app(argc, argv);QPixmap pixmap("splash.png"); //Insert your splash page image here if(pixmap.isNull()) { QMessageBox::warning(0, "Error", "Failed to load Splash Screen image!"); } QSplashScreen splash(pixmap, Qt::WindowStaysOnTopHint); 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(); QObject::connect(thread, SIGNAL(finished()), &loop, SLOT(quit())); QObject::connect(thread, SIGNAL(terminated()), &loop, SLOT(quit())); 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();
}@
@class MyInitThread : public QThread
{
protected:
void run(void)
{
/* Do whatever needs to be done to init your application! */QThread::msleep(5000); //Sleep 5 sec to simulate work ;-) }
};@
-
@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 ? -
C++ does not force you to put each class in a separate .cpp files (with a corresponding .h file), but it usually is a good idea. If you want to, you can put everything into a single file of course. You can name your files as you like.
-
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 1Do 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>class I : public QThread
{
protected:
static void sleep(unsigned long secs) {
QThread::sleep(secs);
}
};int main(int argc, char *argv[])
{
QApplication app(argc, argv);QPixmap pixmap("splash.png"); //Insert your splash page image here
QSplashScreen splash(pixmap);
splash.show();
splash.showMessage(QObject::tr("Initiating your program now..."),
Qt::AlignLeft | Qt::AlignTop, Qt::black); //This line represents the alignment of text, color and positionapp.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>class MyInitThread : public QThread
{
protected:
void run(void)
{
/* Do whatever needs to be done to init your application! */
}
};int main(int argc, char *argv[])
{
QApplication app(argc, argv);QPixmap pixmap("splash.png"); //Insert your splash page image here
QSplashScreen splash(pixmap);
splash.show();
app.processEvents(); //Make sure splash screen gets drawn ASAPQEventLoop 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":https://qt-project.org/forums/viewthread/10075/ 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[])
{
QApplication a(argc, 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. QPixmap pixmap(":/images/splash.png"); SplashScreen splash(true, pixmap, Qt::WindowStaysOnTopHint); // connect loader's progress notifications to splashscreen QObject::connect(w.loader(), SIGNAL(progress(QString,int,QColor)), &splash, SLOT(showMessage(QString,int,QColor))); // on loader done signal, close splashscreen and show mainwindow QObject::connect(w.loader(), SIGNAL(done()), &splash, SLOT(close()));
#if defined(Q_WS_S60)
QObject::connect(w.loader(), SIGNAL(done()), &w, SLOT(startUpMaximized()));
#else
QObject::connect(w.loader(), SIGNAL(done()), &w, SLOT(startUpMaximized()));
#endifsplash.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>
#endifDummyLoader::DummyLoader(QObject *parent)
: QObject(parent),
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; } QMap<int, QString>::const_iterator i = m_states.lowerBound(m_progress); if ( i != m_states.constEnd() ) { QString progressMsg = QString("%1...").arg(i.value()); emit progress(progressMsg, Qt::AlignLeft | Qt::AlignTop, Qt::red); }
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
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.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
}void MainWindow::startUpMaximized()
{
// release orientation lock (if set)
releaseOrientation();setWindowFlags(windowFlags() | Qt::WindowSoftkeysVisibleHint); 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>class DummyLoader : public QObject
{
Q_OBJECTpublic:
explicit DummyLoader(QObject *parent = 0);signals:
void done();
void progress(const QString&, int, const QColor&);public slots:
void doSomething();private:
int m_progress;
QMap<int, QString> m_states;
QTimer m_timer;
};class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget parent = 0);
~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>
class SplashScreen : public QSplashScreen
{
Q_OBJECTpublic:
explicit SplashScreen(bool blocking = false, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0)
: QSplashScreen(pixmap,f), m_blocking(blocking) {}protected: // reimp
void mousePressEvent ( QMouseEvent *event ) {
if(!m_blocking)
QSplashScreen::mousePressEvent(event);
}private:
bool m_blocking;};
#endif // SPLASHSCREEN_H
@data.qrc
@<RCC>
<qresource prefix="/">
<file>images/splash.png</file>
</qresource>
</RCC>
@Also need to create a mainwindow.ui file using Qt Designer with dimensions 600x400
-
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 600x400. 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.
-
[quote]I am trying to figure out how to maximize the main application window to fit the whole computer screen.[/quote]
Try QWidget::resize(). Or use QWidget::setWindowState() with Qt::WindowMaximized or Qt::WindowFullScreen.
-
Okay I went back and tweaked some of the code, I added the following lines to the main.cpp file
@
QObject::connect(w.loader(), SIGNAL(done()), &w, SLOT(startUpMaximized()));
@
It worked it kicked out the main application page to full screen. Look at line #30 in the main.cpp file.So far I tested this out on Ubuntu Linux and Windows and it worked perfectly.
-
One issue I can not figure out is why some coders refuse to post the #include directives and the full source code with their shared code, it is always snippets.
How else is newbie coder going to figure out how to use the shared code in their application? I find this very frustrating.
-
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 ;)
-
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.
-
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
makeThis 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
@
#include <QtCore>
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>
#include <QWidget>
#include <QMainWindow>
#include <QTimer>
#include <QThread>
#include <QDesktopWidget>
#include <QPainter>int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QMainWindow* viewer = new QMainWindow();
QImage splashScrImage ("splash.png");
QSize screenSize = QApplication::desktop()->geometry().size();
QImage splashScr (screenSize, QImage::Format_ARGB32_Premultiplied);
QPainter painter (&splashScr);
painter.fillRect(splashScr.rect(), Qt::black);
QImage scaled = splashScrImage.scaled(screenSize, Qt::KeepAspectRatio);
QRect scaledRect = scaled.rect();
scaledRect.moveCenter(splashScr.rect().center());
painter.drawImage(scaledRect, scaled);
QPixmap Logo;
Logo.convertFromImage(splashScr);
QSplashScreen splashScrWindow (viewer, Logo, Qt::WindowStaysOnTopHint);
splashScrWindow.move(0,0);
splashScrWindow.show();
QTimer::singleShot(3000, &splashScrWindow ,SLOT(close()));
viewer->show();
return app.exec();
}
@data.qrc
@<RCC>
<qresource prefix="/">
<file>splash.png</file>
</qresource>
</RCC>
@ -
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.
-
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.