Can i add widgets to a layout without breaking its stylesheet in pyqt5?

21 views Asked by At

I have made a few buttons and edited them with a stylesheet giving them a round border and changing their size and color. And i need to add a lot of them on a window so i am using a layout.But when they are in a layout the style i have made is not applying to the buttons.

Example:

import sys
from PyQt5.QtWidgets import *


class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Example")
        self.resize(400, 600)
        self.setStyleSheet('''QPushButton 
        {
            border: 2px solid black;
            border-radius: 20;
            color: red;
        }''')

        btn1 = QPushButton("Left-Most")
        btn1.setGeometry(100, 150, 100, 100)
        btn2 = QPushButton("Center")
        btn2.setGeometry(200, 150, 100, 100)
        btn3 = QPushButton("Right-Most")
        btn3.setGeometry(300, 150, 100, 100)

        layout = QVBoxLayout()

        layout.addWidget(btn1)
        layout.addWidget(btn2)
        layout.addWidget(btn3)

        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

This is how to changes when it gets in a layout

Is there ant way i can keep their style so that it looks like this ??How i want it to be even when its in a layout

0

There are 0 answers