0

I have the code snippet below. Basically I'm aiming to create a stacked layout with different UIs for each. I am currently creating my main window from another class. However, I noticed that when I put a background-image using stylesheets, I override the background color of my pushbutton and it only shows on the border resulting to:

Resulting Image

In the image above: red is the color of my background and blue is the supposedly background color of my push button. I'm trying to avoid using pallettes as from the docs using it with stylesheets can result to some buggy behavior

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")
        main_window = MainWindow()
        width, height = 480, 720
        self.setFixedSize(width, height)

        # Center to the screen
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


    # Create a stack object
    self.stacked_layout = QStackedLayout()

    # Set central widget
    self.central_widget = QWidget(self)
    self.central_widget.setLayout(self.stacked_layout)
    self.setCentralWidget(self.central_widget)

    # Add the main window widget to the stack
    self.stacked_layout.addWidget(main_window)


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setAttribute(Qt.WA_StyledBackground)
        self.setStyleSheet("background-image: url(resources/images/red_bg.jpg)")

        # Create start button
        self.startbtn = QPushButton("START", self)
        self.startbtn.setFont(font.set_font('Roboto', 15))
        self.startbtn.clicked.connect(QApplication.instance().quit)
        self.startbtn.setStyleSheet("color: white; background-color:blue")
        self.startbtn.installEventFilter(self)
        self.startbtn.move(210, 405)
Fer-de-lance
  • 375
  • 5
  • 23
  • 2
    change `self.setStyleSheet("background-image: url(resources/images/red_bg.jpg)")` to `self.setObjectName("widget") self.setStyleSheet("#widget{background-image: url(resource/images/red_bg.jpg)}")` – eyllanesc Jun 19 '19 at 07:14
  • 3
    QSS has propagation rules, when you establish a QSS the effect will be propagated to your children by default, so that it does not happen you must use selectors. For more information read: https://doc.qt.io/Qt-5/stylesheet-syntax.html – eyllanesc Jun 19 '19 at 07:15
  • Thank you again for helping. I think you should post it as an answer. It solved my problem. – Fer-de-lance Jun 19 '19 at 07:24
  • It is a duplicate, the same indicates the answer of the other publication so there is no need to write, I have only adapted it to your code – eyllanesc Jun 19 '19 at 07:25

0 Answers0