5

New to PyQt5... Here is a very basic question.

I would like to add an image inside the layout of a widget. This widget is the Main Window / root widget of my application. I use the following code, but I get an error message.

import sys

from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import *

class MainWindow(QWidget):

    def __init__(self):

       super().__init__()

       self.setGeometry(300,300,300,220)
       self.setWindowTitle("Hello !")

       oImage = QImage("backgound.png")

       oLayout = QVBoxLayout()
       oLayout.addWidget(oImage)

       self.setLayout(oLayout)

       self.show()

if __name__ == "__main__":

    app = QApplication(sys.argv)

    oMainwindow = MainWindow()

    sys.exit(app.exec_())


TypeError: QBoxLayout.addWidget(QWidget, int stretch=0, Qt.Alignment alignment=0): argument 1 has unexpected type 'QImage'

Apparently a QLayoutWidget does not accept a QImage as an input. Is there a workaround to have an image appear as a brackground in a QWidget ?

martin_0004
  • 357
  • 1
  • 2
  • 15

1 Answers1

9

The QVBoxLayout class lines up widgets vertically.

documentation QVBoxLayout

QImage is no widget.

on many widgets e.g. QmainWindow, QLabel you can use

widget.setStyleSheet(„ background-image: url(backgound.png);“)

on my machine this doesn't work with QWidget. In this case you can use the following rewrite of your code:

import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QImage, QPalette, QBrush
from PyQt5.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self):
       QWidget.__init__(self)
       self.setGeometry(100,100,300,200)

       oImage = QImage("test.png")
       sImage = oImage.scaled(QSize(300,200))                   # resize Image to widgets size
       palette = QPalette()
       palette.setBrush(QPalette.Window, QBrush(sImage))                        
       self.setPalette(palette)

       self.label = QLabel('Test', self)                        # test, if it's really backgroundimage
       self.label.setGeometry(50,50,200,50)

       self.show()

if __name__ == "__main__":

    app = QApplication(sys.argv)
    oMainwindow = MainWindow()
    sys.exit(app.exec_())
Marc Dirven
  • 309
  • 2
  • 18
a_manthey_67
  • 4,046
  • 1
  • 17
  • 28
  • The code with setPalette works well with my MainWindow. Now the MainWindow also contains a QTabWiget and I am trying to set images in the background of these QTabWidget pages. I tried with setPalette and setStyleSheet, but none seemt work. Is it possible to set images as backgrounds of other widgets, not only windows ? The following do not work: `MyPage.setStyleSheet("background-image: url(myimage.jpg);")` and `MyPage.setPalette(palette)` – martin_0004 Feb 17 '16 at 19:45
  • I can setStyleSheet on QTabWidget as well as on a page (QWidget). If you can't solve it, you should ask a new question with your code. – a_manthey_67 Feb 17 '16 at 21:16
  • I created a new question with my new code here. http://stackoverflow.com/questions/35468937/pyqt5-image-as-background-for-qtabwidgetpage – martin_0004 Feb 17 '16 at 22:21
  • I tried this code but the background image is not displayed, instead it shows black screen with my buttons on the screen. Any solution ? – Rajat Apr 18 '18 at 13:50
  • do you have test.png in your working dir? are there any error messages in console? – a_manthey_67 Apr 18 '18 at 15:00
  • resurrecting old thread but i also only have a black image, the background is loaded properly with a test and the image is in the working dir – Flying Thunder May 18 '20 at 10:46
  • code is still working for me if "test.png" is in working dir. i get a black sreen and the message "QImage::scaled: Image is a null image" if there is no "test.png". – a_manthey_67 May 18 '20 at 17:27