Reputation: 90
I'm working on an application where the background of a widget is an image and I want it to fill the entire widget in the case of a resizeEvent.
Firstly, I will also use a stylesheet not in the same widget but surely in its child widgets, it will not cause a problem if I use a palette at the same time to set the background of the widget. Second, can I do this with a stylesheet? If not I tried with a palette but it didn't work, this is what I tried to do:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import sys
MainUI, _ = loadUiType(R'to_do_app.ui')
class Window(QMainWindow,Ui_MainWindow) :
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
def resizeEvent(self, e):
image = QPixmap('instagrame.jpg')
image.scaled(e.size(),Qt.IgnoreAspectRatio)
palette = self.centralwidget.palette()
brush = QBrush(image)
palette.setBrush(QPalette.Background,brush)
self.centralwidget.setPalette(palette)
self.repaint()
if __name__ =='__main__':
app= QApplication(sys.argv)
window =Window()
window.show()
sys.exit(app.exec())
to_do_app.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>476</width>
<height>598</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
</widget>
</widget>
<resources>
<include location="background_ma_journe.qrc"/>
</resources>
<connections/>
</ui>
qrc file
<RCC>
<qresource prefix="new_image">
<file>Instagram.jpg</file>
</qresource>
</RCC>
the used image
Upvotes: 1
Views: 1032
Reputation: 243907
The OP's code has several inconsistencies (for example what is Push_Button_, page_2, etc) so I will not refer to the cause of the error.
It is not necessary to use .qrc since for example the QPixmap does not use that resource so I will remove the stylesheet and the qresource from the .ui. On the other hand, you must set the background by enabling the autoFillBackground property of the centralWidget.
├── Instagram.jpg
├── main.py
└── to_do_app.ui
to_do_app.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>476</width>
<height>598</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
</widget>
</widget>
<connections/>
</ui>
main.py
import os
import sys
from pathlib import Path
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush, QPalette, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUiType
CURRENT_DIRECTORY = Path(__file__).resolve().parent
MainUI, _ = loadUiType(os.fspath(CURRENT_DIRECTORY / "to_do_app.ui"))
class Window(QMainWindow, MainUI):
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
self.centralwidget.setAutoFillBackground(True)
def resizeEvent(self, event):
super().resizeEvent(event)
pixmap = QPixmap(os.fspath(CURRENT_DIRECTORY / "Instagram.jpg"))
palette = self.centralwidget.palette()
brush = QBrush(pixmap.scaled(event.size(), Qt.IgnoreAspectRatio))
palette.setBrush(QPalette.Window, brush)
self.centralwidget.setPalette(palette)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
Upvotes: 2