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. QSplashScreen, center pixmap
Forum Updated to NodeBB v4.3 + New Features

QSplashScreen, center pixmap

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 677 Views 3 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I am creating a QSplashScreen using configuration specified in an XML file, here is the code so far:

                const QString cstrImage(strGetAttribute(clsXMLnode::mscszAttrImage));
                const QPixmap cpmImage(cstrImage);
                QSplashScreen* pobjSplash(new QSplashScreen(cpmImage, Qt::WindowStaysOnTopHint));
                Q_ASSERT_X(pobjSplash!=nullptr, cpszClassConstr, "Cannot create splash screen!");
                QRect rctGeom(rctGetGeometry()), rctSplash(pobjSplash->geometry());
                if ( rctGeom.isValid() == true ) {
        //Apply the configured geometry to the splash
                    const int cintWidth(rctGeom.width());
                    if ( rctSplash.width() < cintWidth ) {
                        rctSplash.setWidth(cintWidth);
                    }
                    const int cintHeight(rctGeom.height());
                    if ( rctSplash.height() < cintHeight ) {
                        rctSplash.setHeight(cintHeight);
                    }
        //Ensure the splash is centered in the desktop
                    rctSplash.moveCenter(clsMainWnd::rctDesktop().center());
                    pobjSplash->setGeometry(rctSplash);
                }
                pobjSplash->show();
                const QString cstrDelay(strGetAttribute(clsXMLnode::mscszAttrDelay));
                unsigned long ulngDelay = cstrDelay.toULong();
                if ( ulngDelay == 0 ) {
                    ulngDelay = clsXMLnode::msculngDefaultSplashTime;
                }
                QTimer::singleShot(ulngDelay, pobjSplash, [=]() {
                    pobjSplash->deleteLater();
                });
    

    The pixmap current appears top left of the splash, if the configuration specifies a splash larger than the image then I adjust the geometry, what I want to do is adjust the horizontal position of the pixmap so it appears in the center horizontally of the splash. Can this be done?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @SGaist , @Axel-Spoerl , I've revisited this and using a blank UI which I want to populate from the XML configuration:

      clsSplash::clsSplash(clsXMLnode* pobjNode) : mpUI(new Ui::clsSplash) {
          const char* cpszClassName = "clsSplash::clsSplash";
          Q_ASSERT_X(pobjNode!=nullptr, cpszClassName, "Node must be valid!");
          //Set-up UI
          mpUI->setupUi(this);
          //Save node
          mpobjNode = pobjNode;
          //Save widget with node
          mpobjNode->setWidget(this);
          //Set window flags
          setWindowFlags(Qt::CustomizeWindowHint
                       | Qt::FramelessWindowHint
                       | Qt::WindowStaysOnTopHint);
          //Set-up geometry
          QRect rctGeom(pobjNode->rctGetGeometry()), rctSplash;
          if ( rctGeom.isValid() == true ) {
          //Apply the configured geometry to the splash
              const int cintWidth(rctGeom.width());
              if ( rctSplash.width() < cintWidth ) {
                  rctSplash.setWidth(cintWidth);
              }
              const int cintHeight(rctGeom.height());
              if ( rctSplash.height() < cintHeight ) {
                  rctSplash.setHeight(cintHeight);
              }
          //Ensure the splash is centered in the desktop
              rctSplash.moveCenter(clsMainWnd::rctDesktop().center());
              setGeometry(rctSplash);
          }
          //Create layout
          QVBoxLayout* pvboxLO(new QVBoxLayout());
          Q_ASSERT_X(pvboxLO!=nullptr, cpszClassName, "Cannot create layout!");
          //Any image to add?
          const QString cstrImage(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrImage));
          if ( cstrImage.isEmpty() != true ) {
              const QPixmap cpmImage(cstrImage);
              QLabel* pobjImage(new QLabel());
              Q_ASSERT_X(pobjImage!=nullptr, cpszClassName, "Cannot create label!");
              pobjImage->setPixmap(cpmImage);
              pvboxLO->addWidget(pobjImage);
          }
          //Any title to add?
          const QString cstrTitle(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrTitle));
          if ( cstrTitle.isEmpty() != true ) {
              QLabel* pobjTitle(new QLabel(cstrTitle));
              Q_ASSERT_X(pobjTitle!=nullptr, cpszClassName, "Cannot create title!");
              pvboxLO->addWidget(pobjTitle);
          }
          //Set-up the user interface
          QRect rctDesktop(clsMainWnd::rctDesktop());
          QString strTemp;
          strTemp = QString::asprintf("%dpx", rctDesktop.width());
          QLabel* pobjWidth(new QLabel(strTemp));
          Q_ASSERT_X(pobjWidth!=nullptr, cpszClassName, "Cannot create desktop width label!");
          pvboxLO->addWidget(pobjWidth);
          strTemp = QString::asprintf("%dpx", rctDesktop.height());
          QLabel* pobjHeight(new QLabel(strTemp));
          Q_ASSERT_X(pobjHeight!=nullptr, cpszClassName, "Cannot create desktop heigh label!");
          pvboxLO->addWidget(pobjHeight);
          //Set-up automatic removal of splash using timer
          const QString cstrDelay(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrDelay));
          unsigned long ulngDelay = cstrDelay.toULong();
          if ( ulngDelay == 0 ) {
              ulngDelay = clsXMLnode::msculngDefaultSplashTime;
          }    
          QTimer::singleShot(ulngDelay, this, [=]() {
              deleteLater();
          });    
          //Set the layout
          setLayout(pvboxLO);
          //Show the window
          show();
      }
      

      In the XML I have a image and title text, I've verified in the debugger that the image and text are read in correctly, however nothing is displayed except a blank UI which has been resized to the correct width and height...can anyone see what I might have missed?

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #8

      @Axel-Spoerl , @SGaist , fixed! the issue was solved by adding this to the bottom:

          //Get central widget
          QWidget* pobjCentral(centralWidget());
          //Set the layout
          pobjCentral->setLayout(pvboxLO);
      

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        From a quick look at the QSplashScreen code, you need to subclass it as the pixmap painting is done in a private method and the current implementation does not allow for customization.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        SPlattenS 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          From a quick look at the QSplashScreen code, you need to subclass it as the pixmap painting is done in a private method and the current implementation does not allow for customization.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @SGaist , thank you, but if the base class doesn't provide the facilities to do this, then if I subclass, would I just put my own implementation in ?

          Kind Regards,
          Sy

          Axel SpoerlA 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @SGaist , thank you, but if the base class doesn't provide the facilities to do this, then if I subclass, would I just put my own implementation in ?

            Axel SpoerlA Offline
            Axel SpoerlA Offline
            Axel Spoerl
            Moderators
            wrote on last edited by
            #4

            @SPlatten
            Hi Sy,
            You can override drawContents() and put your own implementation there.
            I think that's the only option.
            Cheers
            Axel

            Software Engineer
            The Qt Company, Oslo

            SGaistS 1 Reply Last reply
            0
            • Axel SpoerlA Axel Spoerl

              @SPlatten
              Hi Sy,
              You can override drawContents() and put your own implementation there.
              I think that's the only option.
              Cheers
              Axel

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Axel-Spoerl the only issue here is that drawContents happens after the pixmap is rendered hence my suggestion. This would mean overriding the events method and do the custom pixmap rendering for QEvent::Paint.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Axel SpoerlA 1 Reply Last reply
              1
              • SGaistS SGaist

                @Axel-Spoerl the only issue here is that drawContents happens after the pixmap is rendered hence my suggestion. This would mean overriding the events method and do the custom pixmap rendering for QEvent::Paint.

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #6

                @SGaist
                Right, overlooked that. Thanks!

                Software Engineer
                The Qt Company, Oslo

                SPlattenS 1 Reply Last reply
                0
                • Axel SpoerlA Axel Spoerl

                  @SGaist
                  Right, overlooked that. Thanks!

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by SPlatten
                  #7

                  @SGaist , @Axel-Spoerl , I've revisited this and using a blank UI which I want to populate from the XML configuration:

                  clsSplash::clsSplash(clsXMLnode* pobjNode) : mpUI(new Ui::clsSplash) {
                      const char* cpszClassName = "clsSplash::clsSplash";
                      Q_ASSERT_X(pobjNode!=nullptr, cpszClassName, "Node must be valid!");
                      //Set-up UI
                      mpUI->setupUi(this);
                      //Save node
                      mpobjNode = pobjNode;
                      //Save widget with node
                      mpobjNode->setWidget(this);
                      //Set window flags
                      setWindowFlags(Qt::CustomizeWindowHint
                                   | Qt::FramelessWindowHint
                                   | Qt::WindowStaysOnTopHint);
                      //Set-up geometry
                      QRect rctGeom(pobjNode->rctGetGeometry()), rctSplash;
                      if ( rctGeom.isValid() == true ) {
                      //Apply the configured geometry to the splash
                          const int cintWidth(rctGeom.width());
                          if ( rctSplash.width() < cintWidth ) {
                              rctSplash.setWidth(cintWidth);
                          }
                          const int cintHeight(rctGeom.height());
                          if ( rctSplash.height() < cintHeight ) {
                              rctSplash.setHeight(cintHeight);
                          }
                      //Ensure the splash is centered in the desktop
                          rctSplash.moveCenter(clsMainWnd::rctDesktop().center());
                          setGeometry(rctSplash);
                      }
                      //Create layout
                      QVBoxLayout* pvboxLO(new QVBoxLayout());
                      Q_ASSERT_X(pvboxLO!=nullptr, cpszClassName, "Cannot create layout!");
                      //Any image to add?
                      const QString cstrImage(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrImage));
                      if ( cstrImage.isEmpty() != true ) {
                          const QPixmap cpmImage(cstrImage);
                          QLabel* pobjImage(new QLabel());
                          Q_ASSERT_X(pobjImage!=nullptr, cpszClassName, "Cannot create label!");
                          pobjImage->setPixmap(cpmImage);
                          pvboxLO->addWidget(pobjImage);
                      }
                      //Any title to add?
                      const QString cstrTitle(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrTitle));
                      if ( cstrTitle.isEmpty() != true ) {
                          QLabel* pobjTitle(new QLabel(cstrTitle));
                          Q_ASSERT_X(pobjTitle!=nullptr, cpszClassName, "Cannot create title!");
                          pvboxLO->addWidget(pobjTitle);
                      }
                      //Set-up the user interface
                      QRect rctDesktop(clsMainWnd::rctDesktop());
                      QString strTemp;
                      strTemp = QString::asprintf("%dpx", rctDesktop.width());
                      QLabel* pobjWidth(new QLabel(strTemp));
                      Q_ASSERT_X(pobjWidth!=nullptr, cpszClassName, "Cannot create desktop width label!");
                      pvboxLO->addWidget(pobjWidth);
                      strTemp = QString::asprintf("%dpx", rctDesktop.height());
                      QLabel* pobjHeight(new QLabel(strTemp));
                      Q_ASSERT_X(pobjHeight!=nullptr, cpszClassName, "Cannot create desktop heigh label!");
                      pvboxLO->addWidget(pobjHeight);
                      //Set-up automatic removal of splash using timer
                      const QString cstrDelay(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrDelay));
                      unsigned long ulngDelay = cstrDelay.toULong();
                      if ( ulngDelay == 0 ) {
                          ulngDelay = clsXMLnode::msculngDefaultSplashTime;
                      }    
                      QTimer::singleShot(ulngDelay, this, [=]() {
                          deleteLater();
                      });    
                      //Set the layout
                      setLayout(pvboxLO);
                      //Show the window
                      show();
                  }
                  

                  In the XML I have a image and title text, I've verified in the debugger that the image and text are read in correctly, however nothing is displayed except a blank UI which has been resized to the correct width and height...can anyone see what I might have missed?

                  Kind Regards,
                  Sy

                  SPlattenS 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @SGaist , @Axel-Spoerl , I've revisited this and using a blank UI which I want to populate from the XML configuration:

                    clsSplash::clsSplash(clsXMLnode* pobjNode) : mpUI(new Ui::clsSplash) {
                        const char* cpszClassName = "clsSplash::clsSplash";
                        Q_ASSERT_X(pobjNode!=nullptr, cpszClassName, "Node must be valid!");
                        //Set-up UI
                        mpUI->setupUi(this);
                        //Save node
                        mpobjNode = pobjNode;
                        //Save widget with node
                        mpobjNode->setWidget(this);
                        //Set window flags
                        setWindowFlags(Qt::CustomizeWindowHint
                                     | Qt::FramelessWindowHint
                                     | Qt::WindowStaysOnTopHint);
                        //Set-up geometry
                        QRect rctGeom(pobjNode->rctGetGeometry()), rctSplash;
                        if ( rctGeom.isValid() == true ) {
                        //Apply the configured geometry to the splash
                            const int cintWidth(rctGeom.width());
                            if ( rctSplash.width() < cintWidth ) {
                                rctSplash.setWidth(cintWidth);
                            }
                            const int cintHeight(rctGeom.height());
                            if ( rctSplash.height() < cintHeight ) {
                                rctSplash.setHeight(cintHeight);
                            }
                        //Ensure the splash is centered in the desktop
                            rctSplash.moveCenter(clsMainWnd::rctDesktop().center());
                            setGeometry(rctSplash);
                        }
                        //Create layout
                        QVBoxLayout* pvboxLO(new QVBoxLayout());
                        Q_ASSERT_X(pvboxLO!=nullptr, cpszClassName, "Cannot create layout!");
                        //Any image to add?
                        const QString cstrImage(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrImage));
                        if ( cstrImage.isEmpty() != true ) {
                            const QPixmap cpmImage(cstrImage);
                            QLabel* pobjImage(new QLabel());
                            Q_ASSERT_X(pobjImage!=nullptr, cpszClassName, "Cannot create label!");
                            pobjImage->setPixmap(cpmImage);
                            pvboxLO->addWidget(pobjImage);
                        }
                        //Any title to add?
                        const QString cstrTitle(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrTitle));
                        if ( cstrTitle.isEmpty() != true ) {
                            QLabel* pobjTitle(new QLabel(cstrTitle));
                            Q_ASSERT_X(pobjTitle!=nullptr, cpszClassName, "Cannot create title!");
                            pvboxLO->addWidget(pobjTitle);
                        }
                        //Set-up the user interface
                        QRect rctDesktop(clsMainWnd::rctDesktop());
                        QString strTemp;
                        strTemp = QString::asprintf("%dpx", rctDesktop.width());
                        QLabel* pobjWidth(new QLabel(strTemp));
                        Q_ASSERT_X(pobjWidth!=nullptr, cpszClassName, "Cannot create desktop width label!");
                        pvboxLO->addWidget(pobjWidth);
                        strTemp = QString::asprintf("%dpx", rctDesktop.height());
                        QLabel* pobjHeight(new QLabel(strTemp));
                        Q_ASSERT_X(pobjHeight!=nullptr, cpszClassName, "Cannot create desktop heigh label!");
                        pvboxLO->addWidget(pobjHeight);
                        //Set-up automatic removal of splash using timer
                        const QString cstrDelay(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrDelay));
                        unsigned long ulngDelay = cstrDelay.toULong();
                        if ( ulngDelay == 0 ) {
                            ulngDelay = clsXMLnode::msculngDefaultSplashTime;
                        }    
                        QTimer::singleShot(ulngDelay, this, [=]() {
                            deleteLater();
                        });    
                        //Set the layout
                        setLayout(pvboxLO);
                        //Show the window
                        show();
                    }
                    

                    In the XML I have a image and title text, I've verified in the debugger that the image and text are read in correctly, however nothing is displayed except a blank UI which has been resized to the correct width and height...can anyone see what I might have missed?

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #8

                    @Axel-Spoerl , @SGaist , fixed! the issue was solved by adding this to the bottom:

                        //Get central widget
                        QWidget* pobjCentral(centralWidget());
                        //Set the layout
                        pobjCentral->setLayout(pvboxLO);
                    

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • SPlattenS SPlatten has marked this topic as solved on

                    • Login

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