ZeZe
ZeZe

Reputation: 167

PyQt MainWindow background is not resizing

Here is the code that I am using:

palette = QtGui.QPalette()
myPixmap = QtGui.QPixmap('test1.jpg')
myScaledPixmap = myPixmap.scaled(self.size(), QtCore.Qt.KeepAspectRatio, transformMode = QtCore.Qt.SmoothTransformation)
palette.setBrush(QtGui.QPalette.Window, myScaledPixmap)
self.setPalette(palette)

The background image does show, it does however not resize when the MainWindow resizes. I have tried both size() and frameSize(). How do I fix this so that the background image resizes?

Upvotes: 0

Views: 865

Answers (1)

Ceppo93
Ceppo93

Reputation: 1046

This line:

palette.setBrush(QtGui.QPalette.Window, myScaledPixmap)

Should be like this:

palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(myScaledPixmap))

If you look at the docs http://pyqt.sourceforge.net/Docs/PyQt4/qpalette.html#setBrush-2 the second argument must be a QBrush, not a QPixmap

The following code is a working example:

from PyQt4 import QtGui, QtCore

class MyWin(QtGui.QWidget):

    def resizeEvent(self, event):
        palette = QtGui.QPalette()
        myPixmap = QtGui.QPixmap('test1.jpg')
        myScaledPixmap = myPixmap.scaled(self.size(), QtCore.Qt.KeepAspectRatio, transformMode = QtCore.Qt.SmoothTransformation)
        palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(myScaledPixmap))
        self.setPalette(palette)


app = QtGui.QApplication([])

win = MyWin()
win.show()

app.exec_()

Upvotes: 1

Related Questions