Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't add a stretched background image with PyQt5
Forum Updated to NodeBB v4.3 + New Features

Can't add a stretched background image with PyQt5

Scheduled Pinned Locked Moved Solved General and Desktop
pyqt5python3stylesheet
4 Posts 2 Posters 10.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trunks10
    wrote on 22 Apr 2016, 09:28 last edited by Trunks10
    #1

    Hi,

    I've been struggling with this for the last 3 days... I would like to simply add a stretched background image to my parent QWidget. I literally tried a dozens of times after searching everywhere but I'm still stuck with this issue. I don't use resources (from qt designer). PSB :

    from PyQt5 import QtCore, QtWidgets, QtGui
    import sys
    
    class Ui_Form(QtWidgets.QWidget):
    
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self) 
    
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(860, 619)
        Form.setStyleSheet("QWidget#Form {border-image: url(space.png) 0 0 0 0 stretch stretch;}") 
    

    And it does not work. But when I execute this:

            Form.setStyleSheet("border-image: url(space.png) 0 0 0 0 stretch stretch;")
    

    All Qwidget's children get the background which is obviously not what I want. But it proves at least that there is no problem with the resource itself.

    I've tried to make it work via Qpalette() and it works better but I don't know how to make the background image stretched =/ :

        image = QtGui.QImage("space.png")
        palette = QtGui.QPalette()
        palette.setBrush(10, QtGui.QBrush(image))     
         
        Form.setPalette(palette)
    

    Any idea ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 22 Apr 2016, 10:44 last edited by mrjj
      #2

      HI and welcome
      Maybe you can "cheat" and use a QLabel ?
      http://stackoverflow.com/questions/10915215/pyqt-qt-how-to-stretch-an-image-in-qlabel-widget

      self.label.setScaledContents(true);

      also as a note:
      Form.setStyleSheet("borde xxx
      have no type selector ("QWidget#..")
      so it applies to all childs of the form.
      "QMainwndow#" would target the form
      http://doc.qt.io/qt-5/stylesheet-examples.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Trunks10
        wrote on 22 Apr 2016, 21:58 last edited by Trunks10
        #3

        Thanks for your reply, you gave me some inspiration.
        I took a look at "Qt Style Sheets Examples" and I could not find how to customize a QWidget so I simply convert my QWidget to a QMainWindow. I got rid of the menu bar and the status bar because I don't need them... everything works fine now with QMainWindow :)

        `class Ui_MainWindow(QtWidgets.QMainWindow):

        def __init__(self):
            super().__init__()       
            self.setupUi(self)
        
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(856, 610)
            MainWindow.setStyleSheet("#MainWindow { border-image: url(space.png) 0 0 0 0 stretch stretch; }")
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")`
        
        1 Reply Last reply
        1
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 22 Apr 2016, 22:00 last edited by
          #4

          super :)
          also using "#MainWindow" makes sure it dont apply to childs as stylesheets does.

          1 Reply Last reply
          1

          1/4

          22 Apr 2016, 09:28

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved