PyQt5 - Add image in background of MainWindow layout
Asked Answered
R

1

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 ?

Richers answered 16/2, 2016 at 2:14 Comment(0)
F
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_())
Fulkerson answered 16/2, 2016 at 12:3 Comment(7)
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)Richers
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.Fulkerson
I created a new question with my new code here. #35469437Richers
I tried this code but the background image is not displayed, instead it shows black screen with my buttons on the screen. Any solution ?Guggenheim
do you have test.png in your working dir? are there any error messages in console?Fulkerson
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 dirTrina
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".Fulkerson

© 2022 - 2024 — McMap. All rights reserved.