Namespaces
Variants
Actions

ScreenSaver Qt Old School Way

Jump to: navigation, search
Article Metadata

Tested with
Devices(s): Emulator / desktop / device

Compatibility
Platform(s): All Qt Supported

Article
Keywords: QWidget
Created: skumar_rao (29 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Note: The recommend way of working with screen-saver is QSplashScreen

Basically we will override QWidget::repaint() to call QApplication::flush(), which gets all the events in the application's event queue processed immediately. This is where the real magic of the splash screen happens, and why we can paint on the screen without having any sort of event loop.

qoldschoolsplashscreen.h

class QOldSchoolSplashScreen : public QWidget
{
public:
QOldSchoolSplashScreen( const QPixmap &pixmap );
 
void setStatus( const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black );
void finish( QWidget *mainWin );
void repaint();
 
protected:
void mousePressEvent( QMouseEvent * );
 
private:
QPixmap pix;
};

qoldschoolsplashscreen.cpp

QOldSchoolSplashScreen::QOldSchoolSplashScreen(const QPixmap &pixmap)
: QWidget(0),
pix( pixmap ) {
 
//setErasePixmap( pix );
resize( pix.size() );
QRect scr = QApplication::desktop()->screenGeometry();
move( scr.center() - rect().center() );
show();
repaint();
}
 
void QOldSchoolSplashScreen::setStatus(
const QString &message
, int alignment
, const QColor &color ) {
 
QPixmap textPix = pix;
QPainter painter( &textPix);
painter.setPen( color );
QRect r = rect();
r.setRect( r.x() + 10, r.y() + 10, r.width() - 20, r.height() - 20 );
painter.drawText( r, alignment, message );
 
repaint();
}
 
 
void QOldSchoolSplashScreen::repaint() {
QWidget::repaint();
QApplication::flush();
}
 
void QOldSchoolSplashScreen::finish( QWidget *) {
close();
}
 
void QOldSchoolSplashScreen::mousePressEvent( QMouseEvent * ) {
hide();
}

Then we will use as

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
 
QPixmap pixmap;
pixmap.load("c:/data/myscreen.png");
 
QOldSchoolSplashScreen *splash = new QOldSchoolSplashScreen( pixmap );
splash->setFont( QFont("Times", 10, QFont::Bold) );
splash->setStatus( "Initializing..." );
a.processEvents();
 
Widget w;
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif
 
splash->finish( &w );
delete splash;
 
return a.exec();
}

if we want the widget (screensaver ) to go away as soon as user clicks on it

    void QOldSchoolSplashScreen::mousePressEvent( QMouseEvent * ) {
hide();
}

--skumar_rao 18:16, 29 November 2010 (UTC)

This page was last modified on 11 October 2012, at 04:18.
122 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved