Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QSplashScreen using a PNG with transparent areas

[Solved] QSplashScreen using a PNG with transparent areas

Scheduled Pinned Locked Moved General and Desktop
27 Posts 7 Posters 30.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    ucomesdag
    wrote on last edited by
    #14

    I got it working by subclassing QSplashScreen, setting QSplashScreen::setPixmap(QPixmap splash); and setting the mask to the subclass, setMask(QPixmap mask); .

    Splash image with a text display:

    !http://img.mobypicture.com/aa470873918e7804b36e6fb037e5b0b0_view.jpg(screencap)!

    main:
    @QPixmap splashImage(":images/splash.png");
    QPixmap splashMask(":images/splashmask.png");

    customSplashScreen *splash = new customSplashScreen(splashImage);
    splash->setMessageRect(QRect::QRect(7, 253, 415, 14), Qt::AlignCenter); // Setting the message position.

    QFont splashFont;
    splashFont.setFamily("Arial");
    splashFont.setBold(true);
    splashFont.setPixelSize(9);
    splashFont.setStretch(125);

    splash->setFont(splashFont);
    splash->setMask(splashMask);
    splash->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
    splash->show();

    /* To intercept mousclick to hide splash screen. Since the
    splash screen is typically displayed before the event loop
    has started running, it is necessary to periodically call. */
    app.processEvents();

    splash->showStatusMessage(QObject::tr("Initializing..."));@

    customSplashScreen.h:
    @#ifndef CUSTOMSPLASHSCREEN_H
    #define CUSTOMSPLASHSCREEN_H

    #include <QSplashScreen>
    #include <QPainter>

    class customSplashScreen
    :public QSplashScreen
    {

    public:
    customSplashScreen(const QPixmap& pixmap);
    ~customSplashScreen();
    virtual void drawContents(QPainter *painter);
    void showStatusMessage(const QString &message, const QColor &color = Qt::black);
    void setMessageRect(QRect rect, int alignment = Qt::AlignLeft);

    private:
    QString message;
    int alignement;
    QColor color;
    QRect rect;
    };

    #endif // CUSTOMSPLASHSCREEN_H@

    customSplashScreen.cpp:
    @#include "customSplashScreen.h"

    customSplashScreen::customSplashScreen(const QPixmap& pixmap)
    {
    QSplashScreen::setPixmap(pixmap);
    };

    customSplashScreen::~customSplashScreen()
    {
    };

    void customSplashScreen::drawContents(QPainter *painter)
    {
    QPixmap textPix = QSplashScreen::pixmap();
    painter->setPen(this->color);
    painter->drawText(this->rect, this->alignement, this->message);
    };

    void customSplashScreen::showStatusMessage(const QString &message, const QColor &color)
    {
    this->message = message;
    this->color = color;
    this->showMessage(this->message, this->alignement, this->color);
    };

    void customSplashScreen::setMessageRect(QRect rect, int alignement)
    {
    this->rect = rect;
    this->alignement = alignement;
    };@

    Write “Qt”, not “QT” (QuickTime).

    1 Reply Last reply
    0
    • D Offline
      D Offline
      d.oberkofler
      wrote on last edited by
      #15

      [quote author="ucomesdag" date="1295004120"]I got it working by subclassing QSplashScreen, setting QSplashScreen::setPixmap(QPixmap splash); and setting the mask to the subclass, setMask(QPixmap mask); .

      Splash image with a text display:

      !http://img.mobypicture.com/aa470873918e7804b36e6fb037e5b0b0_view.jpg!
      [/quote]

      Thank you very much!
      Your approach look very promising.
      How exactly did you setup (alpha channel) the two images you are using?

      1 Reply Last reply
      0
      • U Offline
        U Offline
        ucomesdag
        wrote on last edited by
        #16

        The splash image:
        !http://img.mobypicture.com/4ffeff271423757e1833f553a292bd1c_view.jpg(splash)!

        and the mask:
        !http://img.mobypicture.com/158962e6c51d76d0bfe042338fb27d6e_view.jpg(mask)!

        Write “Qt”, not “QT” (QuickTime).

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #17

          Do you mind creating a wiki article in the "HowTo":http://developer.qt.nokia.com/wiki/Category:HowTo and/or "Code snippets":http://developer.qt.nokia.com/wiki/Category:snippets category? It would fit very well there!

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • U Offline
            U Offline
            ucomesdag
            wrote on last edited by
            #18

            [quote author="Volker" date="1295008017"]Do you mind creating a wiki article in the "HowTo":http://developer.qt.nokia.com/wiki/Category:HowTo and/or "Code snippets":http://developer.qt.nokia.com/wiki/Category:snippets category? It would fit very well there![/quote]

            Will do so!

            Write “Qt”, not “QT” (QuickTime).

            1 Reply Last reply
            0
            • U Offline
              U Offline
              ucomesdag
              wrote on last edited by
              #19

              Wiki: "splashscreen snippet":http://developer.qt.nokia.com/wiki/Custom_splashscreen_with_text

              Write “Qt”, not “QT” (QuickTime).

              1 Reply Last reply
              0
              • D Offline
                D Offline
                d.oberkofler
                wrote on last edited by
                #20
                • I have an image with an Alpha channel: am I correct in the assumption that the image (splash.png) you've used has no alpha channel and the mask (splashmask.png) is the (1 bit) alpha channel?

                • If the above assumptions are correct I still seem to have problems with the semi-transparent parts of my image. In my case there is a shadow that uses a multibit alpha channel that is not shown correctly.

                • For now I'm only able to show my splash screen properly using the following code:
                  @QPixmap aPixmap(":/splash.png");
                  QLabel* aWidget = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
                  aWidget->setAttribute(Qt::WA_TranslucentBackground);
                  aWidget->setPixmap(aPixmap);
                  aWidget->show();
                  @

                • Is there maybe a way to have QSplashScreen show the images in the same way as QLabel does when using the Qt::WA_TranslucentBackground attribute ?

                • Is there maybe a way to to use QLabel as an alternative to QSplashScreen and add something similar to showMessage to the QLabel ?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dangelog
                  wrote on last edited by
                  #21

                  [quote author="Dieter" date="1295332434"]

                  • Is there maybe a way to have QSplashScreen show the images in the same way as QLabel does when using the Qt::WA_TranslucentBackground attribute ?[/quote]

                  What do you mean? Did you read the BR I posted a link to?

                  [quote]

                  • Is there maybe a way to to use QLabel as an alternative to QSplashScreen and add something similar to showMessage to the QLabel ?
                    [/quote]

                  Use an ordinary QWidget with a QVBoxLayout holding two QLabels?

                  Software Engineer
                  KDAB (UK) Ltd., a KDAB Group company

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    d.oberkofler
                    wrote on last edited by
                    #22

                    [quote author="peppe" date="1295366429"]What do you mean? Did you read the BR I posted a link to?[/quote]
                    Absolutely! I'm just trying to find a solution to my specific problem.

                    [quote author="peppe" date="1295366429"]Use an ordinary QWidget with a QVBoxLayout holding two QLabels?[/quote]
                    I had some problems with the Qt::WA_TranslucentBackground attribute in QWidget but QFrame seems to work as expected and I will very soon post a solution that worked for me.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      d.oberkofler
                      wrote on last edited by
                      #23

                      It took me a while but I have finally found a very simple and perfect solution for my problem with semitransparent images using an alpha channel with more then 1 bit.

                      It all comes down to use a QFrame with the Qt::WA_TranslucentBackground attribute and emulate the functionality of QSplashScreen.

                      I have tried to mimic as much as possible the functionality and apperance of the original QSplashScreen class but left out some methods to keep this example simple.

                      • This is the new CSplashScreen class that must be used instead of QSplashScreen:

                      @
                      class CSplashScreen : public QFrame
                      {
                      public:
                      CSplashScreen(const QPixmap& pixmap);

                      void clearMessage();
                      void showMessage(const QString& theMessage, int theAlignment = Qt::AlignLeft, const QColor& theColor = Qt::black);

                      private:
                      virtual void paintEvent(QPaintEvent* pe);

                      QPixmap itsPixmap;
                      QString itsMessage;
                      int itsAlignment;
                      QColor itsColor;
                      };
                      @

                      • The methods in CSplashScreen:

                      @
                      CSplashScreen::CSplashScreen(const QPixmap& thePixmap)
                      : QFrame(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint)
                      , itsPixmap(thePixmap)
                      {
                      setAttribute(Qt::WA_TranslucentBackground);
                      setFixedSize(itsPixmap.size());
                      };

                      void CSplashScreen::clearMessage()
                      {
                      itsMessage.clear();
                      repaint();
                      }

                      void CSplashScreen::showMessage(const QString& theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor& theColor/* = Qt::black*/)
                      {
                      itsMessage = theMessage;
                      itsAlignment = theAlignment;
                      itsColor = theColor;
                      repaint();
                      }

                      void CSplashScreen::paintEvent(QPaintEvent* pe)
                      {
                      QRect aTextRect(rect());
                      aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10);

                      QPainter aPainter(this);
                      aPainter.drawPixmap(rect(), itsPixmap);
                      aPainter.setPen(itsColor);
                      aPainter.drawText(aTextRect, itsAlignment, itsMessage);
                      }
                      @

                      A simple function to the CSplashSCreen:

                      @
                      void
                      test_SplashScreen()
                      {
                      class SleeperThread : public QThread
                      {
                      public:
                      static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
                      };

                      QPixmap aSplashImage(":/splash.png");

                      CSplashScreen* aSplashScreen = new CSplashScreen(aSplashImage);
                      aSplashScreen->show();

                      for (int i = 1; i <= 5; i++)
                      {
                      aSplashScreen->showMessage(QString("Processing %1...").arg(i), Qt::AlignTop | Qt::AlignLeft, Qt::white);
                      SleeperThread::msleep(1000);
                      }

                      delete aSplashScreen;
                      }
                      @

                      • Example:
                        !http://img228.imageshack.us/img228/4619/csplashscreen.png!
                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        d.oberkofler
                        wrote on last edited by
                        #24

                        Before marking this topic as solved, I have now also added a complete example "CSplashScreen":http://developer.qt.nokia.com/wiki/QSplashScreen_replacement_for_semitransparent_images" to the "Code snippets":http://developer.qt.nokia.com/wiki/Category:snippets WiKi.

                        Thank you all for all your help!

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #25

                          Hi Dieter,

                          just one note, I would make the QPainter instance an object on the stack.
                          And second: are you sure you want to call the default implementation for the paint event? you do all painting yourselve, don't you?

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            d.oberkofler
                            wrote on last edited by
                            #26

                            [quote author="Gerolf" date="1295535937"]Hi Dieter,
                            just one note, I would make the QPainter instance an object on the stack.
                            And second: are you sure you want to call the default implementation for the paint event? you do all painting yourselve, don't you?[/quote]

                            I agree (and have already changed the code snippets)

                            1 Reply Last reply
                            0
                            • E Offline
                              E Offline
                              edoardo
                              wrote on last edited by
                              #27

                              Updated link to "CSplashScreen":http://qt-project.org/wiki/QSplashScreen-Replacement-for-Semitransparent-Images code snippets

                              --
                              "You were not made to live your lives as brutes, | but to be followers of virtue"

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved