ymmx
ymmx

Reputation: 4967

How add a background image on a QWidget and add positional QLineedit on top of it?

I'm trying to set a background image on a QWidget and have some QlineEdit on top of it.

So for know I have this code

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class Model_GUI(QMainWindow):
    def __init__(self, parent= None ):
        super(Model_GUI, self).__init__()

        self.left = 0
        self.top = 0
        self.width = 800
        self.height = 800
        self.resize(self.width,self.height)

        GB = QGroupBox(" Gain ")
        GB_layout = QHBoxLayout()
        label = QLabel('A')
        edit  = QLineEdit('1')
        GB_layout.addWidget(label)
        GB_layout.addWidget(edit)
        GB.setLayout(GB_layout)

        GB2 = QGroupBox(" Gain ")
        GB_layout2 = QHBoxLayout()
        label2 = QLabel('A')
        edit2 = QLineEdit('1')
        GB_layout2.addWidget(label2)
        GB_layout2.addWidget(edit2)
        GB2.setLayout(GB_layout2)

        #Graph
        Graph = graph()

        self.CentralWidget = QWidget()
        self.globallayout = QHBoxLayout()
        self.globallayout.addWidget(GB)
        self.globallayout.addWidget(Graph)
        self.globallayout.addWidget(GB2)

        self.CentralWidget.setLayout(self.globallayout)
        self.setCentralWidget(self.CentralWidget)


class graph(QWidget):

    def __init__(self):
        super().__init__()
        self.setFixedSize(600,600)

        oImage = QImage("img.png")
        sImage = oImage.scaled(QSize(self.width(), self.height()))  # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))  # 10 = Windowrole
        self.setPalette(palette)

        self.label1 = QLabel('Param1', self)
        self.edit1 = QLineEdit('1', self)
        self.label2 = QLabel('Param2', self)
        self.edit2 = QLineEdit('10', self)
        self.label1.move(50, 50)
        self.edit1.move(500, 50)
        self.label2.move(50, 500)
        self.edit2.move(500, 500)


def main():
    app = QApplication(sys.argv)
    ex = Model_GUI(app)
    ex.setWindowTitle('window')
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

but when I execute it I don't have the image in the QWidget (in the middle).

enter image description here

If I replace ex = Model_GUI(app)with ex = graph(), I have the correct expectation :

enter image description here

I don't understand why the image is correctly set when I'm using the QWidget alone but it isn't set right when I embedded it in a QMainWindow?

Upvotes: 0

Views: 479

Answers (1)

musicamante
musicamante

Reputation: 48374

QWidgets use their QPalette.Window role only if they are top level widgets (as in "windows"), otherwise the parent background is used instead unless the autoFillBackground property (which is false by default) is true.

Just set the property in the widget initialization:

self.setAutoFillBackground(True)

Upvotes: 2

Related Questions