SoFunction
Updated on 2024-11-20

PyQt5 how to set the QWidget window background image problem

PyQt5 Setting QWidget Window Background Image

QWidget add background image problem

Windows created by QWidgets sometimes do not have the ability to set the style of parts of the window directly with setStyleSheet.

For example, if you set up a background image in Qt Designer, you can set up QWidget#Form{ ... } and see the effect.

But after converting to python3 code, running the program doesn't show the background image.

If the style uses background-image, it's fine.

Replace it directly with the following code, i.e. redraw the background image using the QPalette control

palette = QPalette()
(, QBrush(QPixmap(":/pic/images/")))  
(palette)

QSS Background Image Style Differences

  • background-image: background image, default size of the original image, empty part of the window is filled with this background image
  • border-image: Stretches with the window size by default
  • image: Default size of the original image, the empty part of the window is not replenished

PyQt to set the window background image, and image adaptive window size change

The first time I used PyQt, because I have been using Python for some time, I need to do the interface for various reasons, I have searched the Internet for a lot of tips, and I chose the simplest one, download PyQt5 and pyqt5_tools. I won't go into details of the specific configuration here.

Once you have configured it, call QT by clicking on Qt Design (your own name) through the following interface:

Click on it to create the QtWidgets interface (test is your own name):

At this time, click save, select the path of the current project, the project directory will be more a .ui file, this time right-click the ui file:

Convert ui files to Python code using pyuic:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
        def setupUi(self, Form):
                (“Form”)
                (400, 300)
                palette = ()
                brush = ((0, 0, 255))
                ()
                (, , brush)
                brush = ((255, 0, 0))
                ()
                (, , brush)
                brush = ((255, 85, 0))
                ()
                (, , brush)
                brush = ((255, 0, 0))
                ()
                (, , brush)
                brush = ((0, 0, 255))
                ()
                (, , brush)
                brush = ((255, 0, 0))
                ()
                (, , brush)
                (palette)
                (Form)
                (Form)
        def retranslateUi(self, Form):
                _translate = 
                (_translate(“Form”, “Form”))

Create a new file at this time:

from  import QApplication, QWidget
from  import QPixmap,QPainter
from test import Ui_Form
import numpy as np
import sys
class mywindow(Ui_Form, QWidget):
        def init(self):
                super(mywindow, self).init()
                (self)
                 = (10)
                (‘pedestrian detection')
                print()
        def paintEvent(self, event):# set background_img
                painter = QPainter(self)
                (())
                pixmap = QPixmap("./img/")# Change the relative paths to your own images
                ((), pixmap)
if name == ‘main':
app = QApplication()
w = mywindow()
()
()
(app.exec_())

Results:

Full screen:

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.