【问题标题】:PyQt MainWindow background is not resizingPyQt MainWindow 背景没有调整大小
【发布时间】:2016-11-21 13:08:24
【问题描述】:

这是我正在使用的代码:

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)

背景图像会显示,但在 MainWindow 调整大小时它不会调整大小。我已经尝试过 size() 和 frameSize()。如何解决此问题以调整背景图像的大小?

【问题讨论】:

  • 这个代码是在窗口调用resizeEvent(resizeEvent)时执行的吗?

标签: python pyqt pyqt4 pyside


【解决方案1】:

这一行:

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

应该是这样的:

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

如果您查看文档 http://pyqt.sourceforge.net/Docs/PyQt4/qpalette.html#setBrush-2,第二个参数必须是 QBrush,而不是 QPixmap

以下代码是一个工作示例:

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_()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2021-01-30
    相关资源
    最近更新 更多