[Interest] Aligning QProgressBar inside a QSplashScreen

Sean Nall techprowlcom at gmail.com
Tue Oct 2 02:30:09 CEST 2012


I am putting a QProgressBar inside a QSplashScreen by subclassing
QSplashScreen. It overrides the drawContents() method.

I thought I had set the geometry correctly, but it renders at both the top
and bottom of the screen. I don't know why. Perhaps there's another way to
align it. The numbers are correct, as the image is 380x284, so a 19 height
progress bar should be 265 pixels down.

Sorry for crappy picture, splash screen wasn't showing up with print screen
button. It's just a 1 color white splash screen at the moment, but as you
can see, progress bar at top and bottom (they're both the same colors, its
the lighting from the camera).

http://i.imgur.com/p1LoJ.jpg

Another issue will be the showMessage() method of QSplashScreen. I want the
message to appear above the progress bar, right-aligned... if anyone has
any ideas how to do that.

splashscreen.cpp

#include "splashscreen.h"

SplashScreen::SplashScreen(QApplication *app, QWidget *parent) :
    QSplashScreen(parent)
{
    this->app = app;

    this->setPixmap(QPixmap(":/images/splashscreen.png"));
    this->setCursor(Qt::BusyCursor);

    // if I dont make it a child, it *only* renders at the top
    progress = new QProgressBar(this);
    progress->setGeometry(0, 265, 380, 19); // puts it at bottom
    progress->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    progress->setValue(0);
    progress->setMaximum(100);
    progress->setEnabled(true);

    this->showMessage("Hello", Qt::AlignBottom);

    connect(progress, SIGNAL(valueChanged(int)), this,
SLOT(progressBarUpdated(int)));
}

void SplashScreen::drawContents(QPainter *painter)
{
    QSplashScreen::drawContents(painter);
    this->progress->render(painter);
}

void SplashScreen::progressBarUpdated(int value)
{
    this->repaint();
    this->app->processEvents();
}

splashscreen.h

#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H

#include <QSplashScreen>
#include <QProgressBar>
#include <QApplication>

class SplashScreen : public QSplashScreen
{
    Q_OBJECT
public:
    explicit SplashScreen(QApplication *app, QWidget *parent = 0);
    QProgressBar *progress;
    QWidget *spacer;
    QApplication *app;

public slots:
    void progressBarUpdated(int value);

protected:
    void drawContents(QPainter *painter);

};

#endif // SPLASHSCREEN_H

main.cpp

#include <QtGui/QApplication>
#include <time.h>

#include "splashscreen.h"
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    srand(time(0));
    QApplication a(argc, argv);

    SplashScreen *splash = new SplashScreen(&a);
    splash->show();

    // snip.. loading a ton of stuff into memory at startup
    // if you're testing this you might have to sleep/timer here iono

    MainWindow w;
    splash->finish(&w);
    w.show();

    return app.exec();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20121001/f7833ac2/attachment.html>


More information about the Interest mailing list