1

I'm going to separate files on pyside6. I divided Widget and separated files in my own way, but the results are different. Why is it different?

import sys
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QHBoxLayout,
    QWidget,
    QVBoxLayout,
    QLabel,
)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Test")
        self.setGeometry(100, 100, 300, 200)
        central_widget = QWidget()

        widgetsss = QWidget()
        title_1 = QLabel("Title 1")
        title_2 = QLabel("Title 2")
        title_3 = QLabel("Title 3")

        layout = QVBoxLayout()
        layout.addWidget(title_1)
        layout.addWidget(title_2)
        layout.addWidget(title_3)
        widgetsss.setLayout(layout)

        widgetsss.setStyleSheet("background-color:blue;")
        layout = QHBoxLayout(central_widget)
        layout.addWidget(widgetsss)

        self.setCentralWidget(central_widget)

Result is

enter image description here

other is

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Test")
        self.setGeometry(100, 100, 300, 200)
        central_widget = QWidget()

        widgetsss = anoterwidget()

        widgetsss.setStyleSheet("background-color:lightgreen;")
        layout = QHBoxLayout(central_widget)
        layout.addWidget(widgetsss)

        self.setCentralWidget(central_widget)


class anoterwidget(QWidget):

    def __init__(self):
        super().__init__()

        title_1 = QLabel("Title 1")
        title_2 = QLabel("Title 2")
        title_3 = QLabel("Title 3")

        layout = QVBoxLayout()
        layout.addWidget(title_1)
        layout.addWidget(title_2)
        layout.addWidget(title_3)
        self.setLayout(layout)

result is

enter image description here

Why is there such a difference?

1 Answer 1

0

Changing colors via set style-sheets can have undesired side effects. In order to control this a bit better, always mention which component you want to modify, e.g. QLabel { #css_goes_here }:

enter image description here

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QVBoxLayout,
    QLabel,
)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Single Class")

        widg = QWidget()

        layout = QVBoxLayout()

        title_1 = QLabel("Title 1")
        title_2 = QLabel("Title 2")
        title_3 = QLabel("Title 3")

        layout.addWidget(title_1)
        layout.addWidget(title_2)
        layout.addWidget(title_3)
        widg.setLayout(layout)

        p = widg.palette()
        p.setColor(self.backgroundRole(), Qt.red)
        widg.setPalette(p)

        widg.setStyleSheet("QLabel {background-color:pink;}")

        self.setCentralWidget(widg)


class MainWindow2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Double Class")
        widg = anoterwidget()
        widg.setStyleSheet("QLabel {background-color:lightgreen;}")
        self.setCentralWidget(widg)


class anoterwidget(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()

        title_1 = QLabel("Title 1")
        title_2 = QLabel("Title 2")
        title_3 = QLabel("Title 3")

        layout.addWidget(title_1)
        layout.addWidget(title_2)
        layout.addWidget(title_3)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window2 = MainWindow2()
    window.show()
    window2.show()
    app.exec()
Sign up to request clarification or add additional context in comments.

5 Comments

You're on the right track, but there are some inaccuracies. First of all, the style sheet aspect is the most important issue, and should be better clarified: in the first case of the OP, since there are no selectors, the container gets the background along with the labels, so the whole area (including the layout margins) gets blue. The difference in layout margins only affects the outer border, and it's caused by the underlying style: when no layout margins are set, QLayout calls the pixelMetric() function of the parent widget style, which may have different results depending on the »
» widget type (or even consider the padding if set on the style sheet). By default, it returns 11 for top level windows, and 9 for container widgets; in your tests you probably called contentsMargins() before reparenting the inner widget. Also note that the selector should also be used in the second case: it doesn't change the result in your case because using QWidget subclasses prevent background drawing unless Qt.WA_StyledBackground is set (or another container type is used). Finally, using palette and stylesheet is discouraged as incompatible, since the stylesheet always takes »
» precedence (it actually internally overrides the palette of the widget whenever any standard color property is set, see this related post) and may cause inconsistencies due to propagation. In your case, using setPalette() didn't affect anything (mostly because you are not calling setAutoFillBackground(True)), but it's still very important to be aware of these aspects. Finally, while not strictly forbidden, we should avoid reentering the loop of a closed QApplication; for such an example, a QDialog.exec() would've probably been a better choice.
Thank you for the clarifications @musicamante and sorry for the presented half-truths. It was a bit of an exploration on my part, as you can easily tell from my answer. I have "reduced" my answer accordingly.
Thank you. It helped me a lot. What I felt was that it's not a common way to put widgets in widgets. I think it's a more efficient way to place them in center widgets with just a layout. But I have a lot of concerns when I want to split the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.