PDA

View Full Version : Splash screen showing for two seconds



Koas
17th October 2008, 12:20
Hi, I've searched for this and haven't found it, so I thought I'd share this little code.

If you want a dummy splash screen (that is, your main window can start immediately but you want the splash screen to stay a couple of seconds) you can use the following code:


#include <QtGui/QApplication>
#include <QSplashScreen>
#include "mainWND.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(":/mainWND/Resources/splash.png");
QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
mainWND w;
w.setWindowOpacity(0);
w.show();
splash.show();
QTimer::singleShot(2000, &splash, SLOT(close()));
QTimer::singleShot(2000, &w, SLOT(slInit()));

return a.exec();
}

And define a slot in your main window called slInit (or whatever you want to call it) where you put the following code:


void mainWND::slInit()
{
setWindowOpacity(1);
}


With this trick you can show your splash screen for some seconds, then make your main window appear. I have only tested it in Windows Vista, any feedback for other platforms would be great.

I hope this helps anyone, this forum has been a great source of info for my Qt programming and I thought I should give something back! :D

SunnySan
17th October 2008, 13:52
you can also add some comments during this time


pSplash->showMessage("Start ...");
pSplash->showMessage("Initialise the Database...");

replace "->" by "." in your case

SunnySan
17th October 2008, 14:04
to do what you have to create an header for main.h ?? isn't it?

oups forget it....
You added the call to the mainwindow. read it too quickly.

Sheng
17th October 2008, 15:55
under my X11, the splashwindow comes with the mainwindow.
Here is the description in QWidget class

"This feature is available on Mac OS X, X11 platforms that support the Composite extension, and Windows 2000 and later.

Note that under X11 you need to have a composite manager running, and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be supported by the window manager you are using."

but you can simply change "w.setWindowOpacity(0);w.show();" to "w.hide()"
can redefine slinit function to "this->show()" and works as expected.



Hi, I've searched for this and haven't found it, so I thought I'd share this little code.

If you want a dummy splash screen (that is, your main window can start immediately but you want the splash screen to stay a couple of seconds) you can use the following code:


#include <QtGui/QApplication>
#include <QSplashScreen>
#include "mainWND.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(":/mainWND/Resources/splash.png");
QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
mainWND w;
w.setWindowOpacity(0);
w.show();
splash.show();
QTimer::singleShot(2000, &splash, SLOT(close()));
QTimer::singleShot(2000, &w, SLOT(slInit()));

return a.exec();
}

And define a slot in your main window called slInit (or whatever you want to call it) where you put the following code:


void mainWND::slInit()
{
setWindowOpacity(1);
}


With this trick you can show your splash screen for some seconds, then make your main window appear. I have only tested it in Windows Vista, any feedback for other platforms would be great.

I hope this helps anyone, this forum has been a great source of info for my Qt programming and I thought I should give something back! :D

SunnySan
17th October 2008, 16:12
thanks
I modified my splash screen
et voila working code below



int main(int argc, char *argv[]) {
QApplication a(argc, argv);
a.connect(&a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );// added test for player

// The splash screen
QSplashScreen *pSplash = new QSplashScreen();
pSplash->setPixmap(QPixmap(":images/Splash.png"));
pSplash->show();
pSplash->showMessage("Start ...");

// main window
MainWindow w;
w.setWindowTitle(QObject::tr(" my title"));
w.hide();// mainwindow disappears

QTimer::singleShot(3000, pSplash, SLOT(close()));// close splash after 4s
QTimer::singleShot(3000, &w, SLOT(slInit()));// mainwindow reappears after 4s


// create DataBase.
pSplash->showMessage("Initialise the Database...");// shows comments
w.databaseobject-> createPlayListTables(); // DB created but need another object in mainwindow!!

pSplash->showMessage("load data...");
w.loadProgamListFromDB(); //load data to application

return a.exec();

}

also into the mainwindow....
....

void mainWND::slInit(){
this->show();
}
.......




thanks
pass the code around

Koas
17th October 2008, 19:40
but you can simply change "w.setWindowOpacity(0);w.show();" to "w.hide()"
can redefine slinit function to "this->show()" and works as expected.


Of course! How didn't I think of that! Maybe it's because I was playing with the setWindowOpacity() function and thought about the splash screen issue...

Anyway, the "hide() then show()" solution is much better, since we don't have to worry about the transparency issues (I think my first code wouldn't work well in Windows 98 for example).

I love these forums, everyday I learn something new! :)