title: Qt C++ - Aligning QProgressBar inside a QSplashScreen
body: <p>I am putting a <code>QProgressBar</code> inside a <code>QSplashScreen</code> by subclassing QSplashScreen.  It overrides the <code>drawContents()</code> method.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p><a href="http://i.imgur.com/p1LoJ.jpg" rel="noreferrer">http://i.imgur.com/p1LoJ.jpg</a></p>
  
  <p>Another issue will be the <code>showMessage()</code> method of QSplashScreen.  I want the message to appear above the progress bar, right-aligned... if anyone has any ideas how to do that.</p>
  
  <p>splashscreen.cpp</p>
  
  <pre><code>#include "splashscreen.h"
  
  SplashScreen::SplashScreen(QApplication *app, QWidget *parent) :
      QSplashScreen(parent)
  {
      this-&gt;app = app;
  
      this-&gt;setPixmap(QPixmap(":/images/splashscreen.png"));
      this-&gt;setCursor(Qt::BusyCursor);
  
      // if I dont make it a child, it *only* renders at the top
      progress = new QProgressBar(this); 
      progress-&gt;setGeometry(0, 265, 380, 19); // puts it at bottom
      progress-&gt;setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
      progress-&gt;setValue(0);
      progress-&gt;setMaximum(100);
      progress-&gt;setEnabled(true);
  
      this-&gt;showMessage("Hello", Qt::AlignBottom);
  
      connect(progress, SIGNAL(valueChanged(int)), this, SLOT(progressBarUpdated(int)));
  }
  
  void SplashScreen::drawContents(QPainter *painter)
  {
      QSplashScreen::drawContents(painter);
      this-&gt;progress-&gt;render(painter);
  }
  
  void SplashScreen::progressBarUpdated(int value)
  {
      this-&gt;repaint();
      this-&gt;app-&gt;processEvents();
  }
  </code></pre>
  
  <p>splashscreen.h</p>
  
  <pre><code>#ifndef SPLASHSCREEN_H
  #define SPLASHSCREEN_H
  
  #include &lt;QSplashScreen&gt;
  #include &lt;QProgressBar&gt;
  #include &lt;QApplication&gt;
  
  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
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include &lt;time.h&gt;
  
  #include "splashscreen.h"
  #include "mainwindow.h"
  
  int main(int argc, char *argv[])
  {
      srand(time(0));
      QApplication a(argc, argv);
  
      SplashScreen *splash = new SplashScreen(&amp;a);
      splash-&gt;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-&gt;finish(&amp;w);
      w.show();
  
      return app.exec();
  }
  </code></pre>