4

My code succeeds in displaying 'Hello World!', however the CSS styling is not applied. I don't know how to do this properly. If anyone has some good explanations on this topic it would be very much appreciated.

The output of my code

from PySide2.QtWidgets import QApplication, QLabel, QWidget

app = QApplication([])

window = QWidget()
window.setStyleSheet('''
    p {
        font-size: 100px;
    }
''')

label = QLabel('<p>Hello World!</p>', parent=window)

window.show()
app.exec_()
1
  • change p { to QLabel { Commented Jun 16, 2020 at 11:24

1 Answer 1

4

First of all, there is a different Qt StyleSheet that applies to all widgets and the html that some widgets support.

The Qt StyleSheets are a simple way to set the style and some properties to the QWidgets and that are based on CSS 2.1 but adapted to Qt.

Instead some widgets support rich text like html, and that is the case of QLabel, in this case the style must be placed inline

Considering there are several solutions:

from PySide2.QtWidgets import QApplication, QLabel, QWidget

app = QApplication([])

label = QLabel('<p style="font-size: 100px">Hello World!</p>', parent=window)

window.show()
app.exec_()
from PySide2.QtWidgets import QApplication, QLabel, QWidget

app = QApplication([])

window = QWidget()
window.setStyleSheet('''
    QLabel {
        font-size: 100px;
    }
''')

label = QLabel('<p>Hello World!</p>', parent=window)

window.show()
app.exec_()
from PySide2.QtWidgets import QApplication, QLabel, QWidget

app = QApplication([])

window = QWidget()

label = QLabel('<p style="font-size: 100px">Hello World!</p>', parent=window)
font = label.font()
font.setPointSize(100)
label.setFont(font)

window.show()
app.exec_()
Sign up to request clarification or add additional context in comments.

Comments

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.