QSplashScreen, center pixmap
-
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?
-
@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?
-
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.
-
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.
-
@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 ?
@SPlatten
Hi Sy,
You can overridedrawContents()and put your own implementation there.
I think that's the only option.
Cheers
Axel -
@SPlatten
Hi Sy,
You can overridedrawContents()and put your own implementation there.
I think that's the only option.
Cheers
Axel@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-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.@SGaist
Right, overlooked that. Thanks! -
@SGaist
Right, overlooked that. Thanks!@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?
-
@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?
-
S SPlatten has marked this topic as solved on