Skip to content

Instantly share code, notes, and snippets.

@Lesik
Last active February 24, 2020 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lesik/45ac04b5fb7355ed684bc3144ce860ad to your computer and use it in GitHub Desktop.
Save Lesik/45ac04b5fb7355ed684bc3144ce860ad to your computer and use it in GitHub Desktop.
PyQt 5 pyqtProperty QSS example
#!/usr/bin/env python3
from PyQt5.QtCore import QRect, pyqtProperty
from PyQt5.QtGui import QPaintEvent, QPainter, QColor, QPen
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import Qt
class Widget(QWidget):
"""
https://www.riverbankcomputing.com/static/Docs/PyQt5/qt_properties.html
"""
def __init__(self):
super().__init__()
self.color = QColor("red")
self.setStyleSheet("""
Square {
qproperty-textcolor: blue;
}
""")
@pyqtProperty('QColor')
def textcolor(self):
return self.color
@acolor.setter
def textcolor(self, value):
self.color = value
def paintEvent(self, event: QPaintEvent) -> None:
painter = QPainter(self)
pen = QPen()
pen.setColor(self.textcolor)
painter.setPen(pen)
rect = QRect(0, 0, painter.device().width(), painter.device().height())
painter.drawRect(rect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment