How to apply style sheet to a custom widget in PyQt

前端 未结 2 1070
悲&欢浪女
悲&欢浪女 2021-01-31 20:52
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import *  
from PyQt4.QtCore import * 

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow,         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-31 21:32

    Firstly: add an actual widget to your example:

        self.widget = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.widget)
    

    Secondly: do yourself a favour, and use triple-quotes:

        self.widget.setStyleSheet("""
            QWidget {
                border: 20px solid black;
                border-radius: 10px;
                background-color: rgb(255, 255, 255);
                }
            """)
    

    The dot-selector in your example is redundant. What it does is specify that only instances of QWidget itself will be selected, as opposed to sub-classes of QWidget. See the StyleSheet Syntax guide in the Qt docs.

提交回复
热议问题