Added workaround for QLCDNumber.

Added example workaround for QLCDNumber, along with an explanation of
the issues styling the LCD number by default with stylesheets.
main
Alex Huszagh 2022-05-07 12:15:49 -05:00
parent 8f3e87f08e
commit 53be089f6a
4 changed files with 144 additions and 0 deletions

View File

@ -7,6 +7,8 @@ There are limitations to what can be styled with stylesheets, as well as rare bu
- [Menu Hover Background Color](#menu-hover-background-color) - [Menu Hover Background Color](#menu-hover-background-color)
- [QDial](#qdial) - [QDial](#qdial)
- [Custom Style](#custom-style) - [Custom Style](#custom-style)
- [QLCDNumber](#qlcdnumber)
- [LCD Color](#lcd-color)
- [QMdiSubwindow](#qmdisubwindow) - [QMdiSubwindow](#qmdisubwindow)
- [Title Bar Icons](#title-bar-icons) - [Title Bar Icons](#title-bar-icons)
- [QSlider](#qslider) - [QSlider](#qslider)
@ -43,6 +45,14 @@ There are limitations to what can be styled with stylesheets, as well as rare bu
<img src="/assets/custom_dial.png" alt="Custom Dial" width="500" height="192"/> <img src="/assets/custom_dial.png" alt="Custom Dial" width="500" height="192"/>
# QLCDNumber
### LCD Color
The LCD display of a `QLCDNumber` cannot be customized via a stylesheet. An example of how to style a `QLCDNumber` is available in [lcd.py](/example/lcd.py). This works out-of-the-box, and can be a drop-in replacement for `QLCDNumber`.
<img src="/assets/custom_lcd.png" alt="Custom Dial" width="500" height="132"/>
# QMdiSubwindow # QMdiSubwindow
### Title Bar Icons ### Title Bar Icons

BIN
assets/custom_lcd.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

123
example/lcd.py Normal file
View File

@ -0,0 +1,123 @@
#!/usr/bin/env python
#
# The MIT License (MIT)
#
# Copyright (c) <2022-Present> <Alex Huszagh>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
'''
dial
====
Example showing how to override the `paintEvent` and `eventFilter`
for a `QDial`, creating a visually consistent, stylish `QDial` that
supports highlighting the handle on the active or hovered dial.
'''
import math
import shared
import sys
parser = shared.create_parser()
parser.add_argument(
'--no-align',
help='''allow larger widgets without forcing alignment.''',
action='store_true'
)
args, unknown = shared.parse_args(parser)
QtCore, QtGui, QtWidgets = shared.import_qt(args)
compat = shared.get_compat_definitions(args)
colors = shared.get_colors(args, compat)
class LCD(QtWidgets.QLCDNumber):
'''QLCDNumber with a custom palette.'''
def __init__(self, widget=None):
super().__init__(widget)
self.setContentsMargins(1, 1, 1, 1)
if args.stylesheet == 'native':
return
# The color of the non-flat LCD numbers is still controlled
# via the `color` stylesheet attribute.
r, g, b, a = colors.HighLightDark.getRgb()
color = (r, g, b, a / 255)
self.setStyleSheet(f'QLCDNumber {{ color: rgba{color}; }}')
palette = self.palette()
palette.setColor(compat.BackgroundPalette, colors.Background)
palette.setColor(compat.LightPalette, colors.Selected)
palette.setColor(compat.DarkPalette, colors.Notch)
self.setPalette(palette)
class Ui:
'''Main class for the user interface.'''
def setup(self, MainWindow):
MainWindow.setObjectName('MainWindow')
MainWindow.resize(1068, 824)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.layout = QtWidgets.QHBoxLayout(self.centralwidget)
self.layout.setSpacing(0)
if not args.no_align:
self.layout.setAlignment(compat.AlignVCenter)
MainWindow.setCentralWidget(self.centralwidget)
self.lcd1 = LCD(self.centralwidget)
self.lcd1.display(15)
self.lcd1.setDigitCount(2)
self.layout.addWidget(self.lcd1)
self.lcd2 = LCD(self.centralwidget)
self.lcd2.display(31)
self.lcd2.setHexMode()
self.lcd2.setDigitCount(2)
self.layout.addWidget(self.lcd2)
self.lcd3 = LCD(self.centralwidget)
self.lcd3.display(15)
self.lcd3.setSegmentStyle(compat.LCDOutline)
self.lcd3.setFrameShape(compat.NoFrame)
self.lcd3.setDigitCount(2)
self.layout.addWidget(self.lcd3)
self.lcd4 = LCD(self.centralwidget)
self.lcd4.display(15)
self.lcd4.setSegmentStyle(compat.LCDFlat)
self.lcd4.setFrameShape(compat.NoFrame)
self.lcd4.setDigitCount(2)
self.layout.addWidget(self.lcd4)
def main():
'Application entry point'
app, window = shared.setup_app(args, unknown, compat)
# setup ui
ui = Ui()
ui.setup(window)
window.setWindowTitle('QLCDNumber')
shared.set_stylesheet(args, app, compat)
return shared.exec_app(args, app, window, compat)
if __name__ == '__main__':
sys.exit(main())

View File

@ -245,6 +245,10 @@ def get_compat_definitions(args):
ns.MouseButtonRelease = ns.EventType.MouseButtonRelease ns.MouseButtonRelease = ns.EventType.MouseButtonRelease
ns.MouseMove = ns.EventType.MouseMove ns.MouseMove = ns.EventType.MouseMove
ns.WindowPalette = ns.ColorRole.Window ns.WindowPalette = ns.ColorRole.Window
ns.WindowTextPalette = ns.ColorRole.WindowText
ns.BackgroundPalette = ns.ColorRole.Background
ns.LightPalette = ns.ColorRole.LightPalette
ns.DarkPalette = ns.ColorRole.DarkPalette
ns.PlaceholderText = ns.ColorRole.PlaceholderText ns.PlaceholderText = ns.ColorRole.PlaceholderText
ns.ToolTipBase = ns.ColorRole.ToolTipBase ns.ToolTipBase = ns.ColorRole.ToolTipBase
ns.ToolTipText = ns.ColorRole.ToolTipText ns.ToolTipText = ns.ColorRole.ToolTipText
@ -526,6 +530,10 @@ def get_compat_definitions(args):
ns.MouseButtonRelease = QtCore.QEvent.MouseButtonRelease ns.MouseButtonRelease = QtCore.QEvent.MouseButtonRelease
ns.MouseMove = QtCore.QEvent.MouseMove ns.MouseMove = QtCore.QEvent.MouseMove
ns.WindowPalette = QtGui.QPalette.Window ns.WindowPalette = QtGui.QPalette.Window
ns.WindowTextPalette = QtGui.QPalette.WindowText
ns.BackgroundPalette = QtGui.QPalette.Background
ns.LightPalette = QtGui.QPalette.Light
ns.DarkPalette = QtGui.QPalette.Dark
ns.PlaceholderText = QtGui.QPalette.PlaceholderText ns.PlaceholderText = QtGui.QPalette.PlaceholderText
ns.ToolTipBase = QtGui.QPalette.ToolTipBase ns.ToolTipBase = QtGui.QPalette.ToolTipBase
ns.ToolTipText = QtGui.QPalette.ToolTipText ns.ToolTipText = QtGui.QPalette.ToolTipText
@ -739,6 +747,7 @@ def get_colors(args, compat):
ns.MidTone = compat.QtGui.QColor(127, 127, 127) ns.MidTone = compat.QtGui.QColor(127, 127, 127)
ns.ViewBackground = compat.QtGui.QColor(0, 0, 0) ns.ViewBackground = compat.QtGui.QColor(0, 0, 0)
ns.TabBackground = compat.QtGui.QColor(0, 0, 0) ns.TabBackground = compat.QtGui.QColor(0, 0, 0)
ns.HighLightDark = compat.QtGui.QColor(255, 0, 0)
if 'dark' in args.stylesheet: if 'dark' in args.stylesheet:
ns.Background = compat.QtGui.QColor(49, 54, 59) ns.Background = compat.QtGui.QColor(49, 54, 59)
ns.Foreground = compat.QtGui.QColor(239, 240, 241) ns.Foreground = compat.QtGui.QColor(239, 240, 241)
@ -754,6 +763,7 @@ def get_colors(args, compat):
ns.MidTone = compat.QtGui.QColor(118, 121, 124) ns.MidTone = compat.QtGui.QColor(118, 121, 124)
ns.ViewBackground = compat.QtGui.QColor(29, 32, 35) ns.ViewBackground = compat.QtGui.QColor(29, 32, 35)
ns.TabBackground = compat.QtGui.QColor(44, 48, 52) ns.TabBackground = compat.QtGui.QColor(44, 48, 52)
ns.HighLightDark = compat.QtGui.QColor(42, 121, 163)
elif 'light' in args.stylesheet: elif 'light' in args.stylesheet:
ns.Background = compat.QtGui.QColor(239, 240, 241) ns.Background = compat.QtGui.QColor(239, 240, 241)
ns.Foreground = compat.QtGui.QColor(49, 54, 59) ns.Foreground = compat.QtGui.QColor(49, 54, 59)
@ -769,6 +779,7 @@ def get_colors(args, compat):
ns.MidTone = compat.QtGui.QColor(186, 185, 184) ns.MidTone = compat.QtGui.QColor(186, 185, 184)
ns.ViewBackground = compat.QtGui.QColor(239, 240, 241) ns.ViewBackground = compat.QtGui.QColor(239, 240, 241)
ns.TabBackground = compat.QtGui.QColor(217, 216, 215) ns.TabBackground = compat.QtGui.QColor(217, 216, 215)
ns.HighLightDark = compat.QtGui.QColor(45, 147, 200, 127)
return ns return ns