Search code examples
pythonqt5pyqt5qpixmapqsplashscreen

QSplashScreen.setPixmap moves Splashscreen back to default position


I created a QSplashScreen with a QPixmap and moved it to the center of my second monitor (not the default monitor):

class SplashScreen(QSplashScreen):
  def __init__(self):
    self._pixmap = QPixmap("test1.png")
    super(SplashScreen, self).__init__(self._pixmap)

    screen = 1
    scr = qApp.desktop().screenGeometry(screen)
    self.move(scr.center() - self.rect().center()) # move to second screen

I'm now trying to add something to my pixmap, while SplashScreen is displayed:

  def drawSomething(self):
    add = QPixmap('test2.png')
    painter = QPainter(self._pixmap)
    painter.drawPixmap(0,0, add)
    #nothing happing so far
    self.repaint() # nothing happening
    self.setPixmap(self._pixmap) # changes shown, but moved back to default-screen

It seems like the QPixmap used to create the QSplashScreen got copied and isn't the same reference anymore, so changes on this have no direct effect.

Furthermore, using setPixmap() moves the SplashScreen back on the default-monitor.


Is there an option to directly paint on the active QPixmap of the Splashscreen or to set a new one without being in need of moving the screen again?

(using the move-command doesn't seems like a good option, when you repaint quickly in a short period of time - the splashscreen then flashes on both monitors)


usage example:

app = QApplication([])
splash = SplashScreen()
splash.show()
splash.drawSomething()
exit(app.exec_())

Solution

  • I finally had success with reimplementing the paintEvent with a QPainter on self:

    def paintEvent(self, *args, **kwargs):
        painter = QPainter(self)
        pixmap = QPixmap('test2.png')
        #self.setMask(pixmap.mask()) # optional for transparency painting
        painter.drawPixmap(0,0, pixmap)
    

    However, reimplementing the drawContents funktion like S.Monteleone suggested worked fine too:

    def drawContents(self, painter):
        painter.drawPixmap(0,0, QPixmap('test2.png'))
        super(SplashScreen, self).drawContents(painter)
    

    Remember to also call super, after your painting, to not lose the drawing of messages shown on the QSplashScreen.

    The default implementation draws the message passed by showMessage().