Issue
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?
Solution
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_()
Answered By - Ceppo93
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.