[Interest] Aligning QProgressBar inside a QSplashScreen
Alex Malyushytskyy
alexmalvtk at gmail.com
Tue Oct 2 05:07:36 CEST 2012
Few comments
1. If you remove :
splash->finish(&w);
You will have splash staying on top until you click it.
Good for debugging.
2. QApplication can only be one, so it does not make sense to pass
pointer of it, use qApp;
3. On Windows with Qt 4.7 your example result in Unhandled exception :
Access violation reading location 0xcdcdcdd1.
at this->progress->render(painter, p);
And the reason is that drawContents is called before constructor finished.
This happens due to the following line in constructor:
this->setPixmap(QPixmap(":/images/splashscreen.png"));
This function is not supposed to be called there cause it trigger draw context.
Move it to the the main before show or use constructor parameters.
You would save a lot of time if u properly initialized this->progress to null
and used Q_CHECK_PTR in drawContents as below
SplashScreen::SplashScreen( QWidget *parent )
: QSplashScreen(parent)
, progress(NULL)
{
.........
void SplashScreen::drawContents(QPainter *painter)
{
QSplashScreen::drawContents(painter);
Q_CHECK_PTR( this->progress );
this->progress->render(painter);
}
Regards,
Alex
On Mon, Oct 1, 2012 at 5:30 PM, Sean Nall <techprowlcom at gmail.com> wrote:
> 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();
>
> }
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
More information about the Interest
mailing list