PDA

View Full Version : Splash Screen



Salazaar
3rd June 2007, 17:45
Hi. I've got a question. In C++ GUI Programming with Qt 4 is explained how to do a splash screen, but it's different than this, what I wanna do. In book it was explained how to do this when you were implementing forms manually, and I was doing forms by designer. And I want to display splash screen without for example "loading plugins", but only and image, which is displaying for 4 seconds, and after it main window is shown. How can I do that, on my main.cpp file:

#include <QApplication>
#include <QPlastiqueStyle>
#include "maker.h"
#include "about.h"

int main(int argc, char *argv[])
{
QApplication::setStyle ( new QPlastiqueStyle() );
QApplication app(argc, argv);
MainWindow MainWindow;
MainWindow.show();
return app.exec();
}
Regards

wysota
3rd June 2007, 18:03
Create the splashscreen, show it, start a timer and when it timeouts, close it. Take a look at QSplashScreen docs to see some examples.

Salazaar
3rd June 2007, 18:44
But the examples in docs doesn't say how to set timer

wysota
3rd June 2007, 19:52
Because they are not meant to close after 4 seconds :) Try reading the docs of a class that has a "timer" word in its name...

Salazaar
3rd June 2007, 19:57
Because they are not meant to close after 4 seconds :) Try reading the docs of a class that has a "timer" word in its name...
I mean to close not application, but spalsh. I've done everything what's described in docs (without timer, yet)

#include <QApplication>
#include <QPlastiqueStyle>
#include <QSplashScreen>
#include "maker.h"
#include "about.h"

int main(int argc, char *argv[])
{
QApplication::setStyle ( new QPlastiqueStyle() );
QApplication app(argc, argv);
QPixmap pixmap("new/prefix1/Poker.jpg");
QSplashScreen splash(pixmap);
splash.show();
app.processEvents();

MainWindow MainWindow;
MainWindow.show();
splash.finish(&MainWindow);
return app.exec();
}
But when I run the program, splash screen doesn't appear. Regards

marcel
3rd June 2007, 20:04
Maybe you have a problem with the jpeg plugin. I have seen a few people getting this problem. Try using a PNG image instead.

As for the timer, do something like this in main after you create the splash:


QTimer splashTimer();
splashTimer.setSingleShot( true );
splashTimer.setInterval( 4000 ); //4 seconds
connect( &splashTimer, SIGNAL( timeOut() ), &splash, SLOT( close() );
splashTimer.start();


Regards

wysota
3rd June 2007, 20:05
Because you close it immediately. Splash screen is usually meant to occupy user's attention when the application takes time to initialize itself. If you only want to show the splashscreen because of eye candy, show the splash, start the timer and close it when the timer timeouts.

Salazaar
3rd June 2007, 20:10
Marcel - i use jpg images for actions, and I works correctly.
[B][/WysotaB] - yes, I want to use it as "eye candy". What do you mean that I close the app immediatly? Splash screen is waiting for my answer, isn't it?

marcel
3rd June 2007, 20:13
Understood.

QSpkashScreen::finish:


Makes the splash screen wait until the widget mainWin is displayed before calling close (http://www.qtcentre.org/forum/qwidget.html#close)() on itself.
This doesn't necessarily mean that you have time to see the splash.
The main window may become visible very quickly, depending on what it contains, what initialization code you have and on the performance of your computer.

Use the timer to make it stay visible for a predefined period of time.

Regards

Salazaar
3rd June 2007, 20:18
I've done this:

#include <QApplication>
#include <QPlastiqueStyle>
#include <QSplashScreen>
#include "maker.h"
#include "about.h"

int main(int argc, char *argv[])
{
QApplication::setStyle ( new QPlastiqueStyle() );
QApplication app(argc, argv);
QPixmap pixmap("new/prefix1/Poker.jpg");
QSplashScreen splash(pixmap);


QTimer splashTimer();

splashTimer.setSingleShot( true );

splashTimer.setInterval( 4000 ); //4 seconds

connect( &splashTimer, SIGNAL( timeOut() ), &splash, SLOT( close() );
splashTimer.start();
splash.show();

MainWindow MainWindow;
MainWindow.show();
splash.finish(&MainWindow);
return app.exec();
}

As you told me, but there appear errors:
http://allyoucanupload.webshots.com/v/2004025593906661174
What's wrong with this code? It should show splash screen for 4 seconds, and after this time if should start application, right?

marcel
3rd June 2007, 20:24
Make sure you include <QTimer>.
Also remove splash.finish() because now the splash closes when the timer time outs.

The behavior is not as you describe it.
The splash will stay visible for 4 seconds but in the mean time the main window may become visible. The splash does not block the main window.

Regards

Salazaar
3rd June 2007, 20:33
The bahavior is exactly the same what I want it to be. I included QTimer, but errors are still the same:
http://allyoucanupload.webshots.com/v/2004025593906661174
Regards

marcel
3rd June 2007, 20:40
You declared splashTimer as:


QTimer splashTimer();
Drop the (), because it may be the problem.

Also, you can select and copy the output from the console and paste it in your reply.
Just select the text, right-click and select Copy.
It is kind of boring to follow all that links to see the screen.

EDIT: that really is the problem. I think you're using gcc. The MS compiler thinks it is a function when declared like that.

Regards

Salazaar
3rd June 2007, 20:56
You did this mistake, though ;) I deleted it, and added QObject:: prefix to connect. And I have a question. Why it's
&splashTimer? What about "&" ?
Application compiled well, but when I launch the program, it doesn't display splash screen. Why? Regards

marcel
3rd June 2007, 20:59
Oh, I did a mistake...Shame on me then.
You could have used the QApplication instance to access connect.

The "&" is there because connect takes a pointers to QObjects as parameters.
Both splash and splashTimer are allocated on the stack, therefore you must pass their address to connect.

I don't know why it does not show...
Have you removed splash.finish()?

Regards

Salazaar
3rd June 2007, 21:02
But without QObject:: prefict is shows error, that connect is undeclared... But I still don't know, why it doesn't show splash screen.
Regards

marcel
3rd June 2007, 21:07
It is OK that you use QObject::connect.
I meant that you could have also used app.connect(...).

But make sure you don't call splash.finish() anymore.

Salazaar
3rd June 2007, 21:23
Yes, I don't call splash.finish(). I deleted it from my main.cpp. So what's the problem with? ;) Regards

marcel
3rd June 2007, 21:27
Are you sure 100% that the image path is correct and the image is loaded OK?
If so, then check if the splash is displayed behind the main window...

Salazaar
3rd June 2007, 21:32
I checked it, splash isn't displayed behind mw. No, I'm not 100% sure. How can I make myself sure? I've got resource file, and location of it new/prefix1/Poker.jpg. But I saw in book, that after quotation mark and before first letter is ":", so
"new/prefix1/Poker.jpg"
would be
":new/prefix1/Poker.jpg"
Is that making things different?

marcel
3rd June 2007, 21:37
":new/prefix1/Poker.jpg"

This is a path in the resource filesystem.
To access the image that way you have to add it to a qrc file under that path, and compile the qrc.

Anyway, make sure you're able to load jpeg files and this jpeg file does not have any weird compression that the Qt plugin is not able to interpret.

Regards

wysota
3rd June 2007, 22:24
The splashscreen is displayed with Qt::WindowStaysOnTopHint so it's on top of other windows.

Salazaar
4th June 2007, 08:36
So I have to use ":". I will try to add it and use png image instead jpg to see if this was a problem.

Salazaar
4th June 2007, 14:12
I added ":" prefix, and selected png image instead jpg, but it still doesn't work. Image's not displayed. Contents of my main.cpp file again

#include <QApplication>
#include <QPlastiqueStyle>
#include <QSplashScreen>
#include <QTimer>
#include "maker.h"
#include "about.h"

int main(int argc, char *argv[])
{
QApplication::setStyle ( new QPlastiqueStyle() );
QApplication app(argc, argv);
QPixmap pixmap(":new/prefix1/poker.png");
QSplashScreen splash(pixmap);


QTimer splashTimer;

splashTimer.setSingleShot( true );

splashTimer.setInterval( 4000 ); //4 seconds

QObject::connect( &splashTimer, SIGNAL( timeOut() ), &splash, SLOT( close()) );
splashTimer.start();
splash.show();

MainWindow MainWindow;
MainWindow.show();

return app.exec();
}

Do someone know, what's wrong?
edit:
all forms were created using designer

wysota
4th June 2007, 14:33
Add "CONFIG+=debug console" to your project file, rebuild, run and see the message about a nonexistent signal "QTimer::timeOut()". It should be "timeout".

marcel
4th June 2007, 17:04
Add "CONFIG+=debug console" to your project file, rebuild, run and see the message about a nonexistent signal "QTimer::timeOut()". It should be "timeout".
Yes, but that does not explain the splash not showing up.

Regards

jpn
4th June 2007, 17:08
Salazaar, could you check what QPixmap::isNull() returns once you've enabled console like Wysota suggested:


#include <QtDebug>
...
qDebug() << pixmap.isNull();

Salazaar
4th June 2007, 17:31
I don't know what do you mean, but I've done it, but there appear errors in compilation that
C:\Dev-cpp\.... cannot find QtGuid4. And I don't know is it this what you were asking about, but:
collect2: ld returned exit status