Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply style sheet to a custom widget in PyQt

# -*- coding: utf-8 -*-

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

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

        self.setFixedWidth(200)
        self.setFixedHeight(200)

        stylesheet = \
            ".QWidget {\n" \
            + "border: 20px solid black;\n" \
            + "border-radius: 4px;\n" \
            + "background-color: rgb(255, 255, 255);\n" \
            + "}"
        self.setStyleSheet(stylesheet)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

I want to add a border to a custom widget with style sheet, but the style sheet does not seem to work, anything wrong?

like image 297
Anke_Pet Avatar asked Mar 19 '14 11:03

Anke_Pet


People also ask

Can I use CSS in PyQt5?

You can use css stylesheets with PyQt. The method call setStyleSheet() gives you the freedom to style the different components in your application.

What is stylesheet in Qt?

Qt Style Sheets are a powerful mechanism that allows you to customize the appearance of widgets, in addition to what is already possible by subclassing QStyle. The concepts, terminology, and syntax of Qt Style Sheets are heavily inspired by HTML Cascading Style Sheets (CSS) but adapted to the world of widgets.

What is a widget in PyQt?

Widgets are the basic building blocks for graphical user interface (GUI) applications built with Qt. Each GUI component (e.g. buttons, labels, text editors) is a widget that is placed somewhere within a user interface window, or is displayed as an independent window.

What is layout in PyQt?

PyQt API provides layout classes for more elegant management of positioning of widgets inside the container. The advantages of Layout managers over absolute positioning are − Widgets inside the window are automatically resized.


2 Answers

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.

like image 181
ekhumoro Avatar answered Sep 22 '22 05:09

ekhumoro


In your project folder add a basic CSS file mystylesheet.css. Mutli-language editors like Atom are best for this type of things. The syntax highlighting works properly if you keep it named a CSS file.

Then drop the dot; qt knows what you mean.

mystylesheet.css

QWidget {
    border: 20px solid black;
    border-radius: 10px;
    background-color: rgb(255, 255, 255);
}
anyQelement.setStyleSheet(open('mystylesheet.css').read())
like image 21
Eric Petersen Avatar answered Sep 20 '22 05:09

Eric Petersen