How do I use QT6 Dark Theme with PySide6?
Asked Answered
R

4

8

Simple demo application I am trying to set the theme to dark. I would prefer a code version (non QtQuick preferred), but only way I see for Python is with a QtQuick config file, and even that does not work.

from PySide6 import QtWidgets
from PySide6 import QtQuick

if __name__ == '__main__':
    app = QtWidgets.QApplication()
    app.setApplicationDisplayName("Should be Dark Theme")
    app.setStyle("Universal")
    view = QtQuick.QQuickView()
    view.show()
    app.exec()

And I have a qtquickcontrols2.conf configuration file in the same directory. (Also tried setting QT_QUICK_CONTROLS_CONF to absolute path.)

[Controls]
Style=Material

[Universal]
Theme=Dark

[Material]
Theme=Dark

And yet, it's still bright white:

not dark theme

I do not care if it is Material or Universal style, just want some built in dark mode for the title bar. In the end, need a way to make the titlebar dark without creating a custom one.

Thank you for any guidance!

Rendition answered 21/7, 2022 at 2:37 Comment(3)
The title bar color is controlled by your OS settings not QT.Tiffanitiffanie
See hereHearthstone
@alexpdev My theme is Windows (Dark) and title bar is still bright whiteRendition
B
11
import sys
sys.argv += ['-platform', 'windows:darkmode=2']
app = QApplication(sys.argv)

above 3 lines can change your window to dark mode if you are using windows and Fusion style makes the app more beautiful, tested in windows 10, 11

example:-

from PySide6.QtWidgets import (
    QApplication,
    QCheckBox,
    QComboBox,
    QDateEdit,
    QDateTimeEdit,
    QDial,
    QDoubleSpinBox,
    QFontComboBox,
    QLabel,
    QLCDNumber,
    QLineEdit,
    QMainWindow,
    QProgressBar,
    QPushButton,
    QRadioButton,
    QSlider,
    QSpinBox,
    QTimeEdit,
    QVBoxLayout,
    QWidget,
)
import sys
sys.argv += ['-platform', 'windows:darkmode=2']


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Widgets App")

        layout = QVBoxLayout()
        widgets = [
            QCheckBox,
            QComboBox,
            QDateEdit,
            QDateTimeEdit,
            QDial,
            QDoubleSpinBox,
            QFontComboBox,
            QLCDNumber,
            QLabel,
            QLineEdit,
            QProgressBar,
            QPushButton,
            QRadioButton,
            QSlider,
            QSpinBox,
            QTimeEdit,
        ]

        for w in widgets:
            layout.addWidget(w())

        widget = QWidget()
        widget.setLayout(layout)

        self.setCentralWidget(widget)


app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
window.show()
app.exec()
Billowy answered 20/11, 2022 at 4:40 Comment(2)
just this did it for me, no need for the args. maybe because i have windows in dark mode? app.setStyle('Fusion')Psychotechnology
It do not work with PySide6-6.5.2, ['-platform', 'windows:darkmode=2'] has no effect even with app.setStyle('Fusion') I allways get light mode. Maybe something change: qt.io/blog/dark-mode-on-windows-11-with-qt-6.5Theocentric
M
2

The former code ion the right track, but its incomplete. This is a working example using PySide6 on Windows. It could use just a little color tweaking to make the unactivated checkbox and radio button more visible.

Palette_Dark_Widgets.PNG

The complete Python 3.9 code is as follows:

# DarkModePalette.PY    2023-04-27 1:14:44 PM
# 
# Complete application based on the "Dark Theme" setup code on pages 233-234 
#   of 'Create GUI Applications with Python Qt6 (PySide6 Edition) V2.0 2021, M. Fitzpatrick'
#   See code example 'palette_dark_widgets.py' in the 'themes' folder.
# Demonstrates an overall visual theme change.
# Note: Code "app.setStyle('Fusion')" is mandatory (on Windows) to produce dark text backgrounds.


from PySide6.QtGui import QPalette, QColor
from PySide6.QtCore import Qt                   # Named colors.

import sys

from PySide6.QtWidgets import (
    QApplication,
    QCheckBox,
    QComboBox,
    QDateEdit,
    QDateTimeEdit,
    QDial,
    QDoubleSpinBox,
    QFontComboBox,
    QLabel,
    QLCDNumber,
    QLineEdit,
    QMainWindow,
    QProgressBar,
    QPushButton,
    QRadioButton,
    QSlider,
    QSpinBox,
    QTimeEdit,
    QVBoxLayout,
    QWidget,
)

#------------------------------------------------

class MainWindow(QMainWindow):
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Dark_Mode.PY Widgets App")

        layout = QVBoxLayout()
        widgets = [
            QCheckBox,
            QComboBox,
            QDateEdit,
            QDateTimeEdit,
            QDial,
            QDoubleSpinBox,
            QFontComboBox,
            QLCDNumber,
            QLabel,
            QLineEdit,
            QProgressBar,
            QPushButton,
            QRadioButton,
            QSlider,
            QSpinBox,
            QTimeEdit,
        ]
        
        for w in widgets:
            widget = w( self )
            widget.setAutoFillBackground( True )    # Doesn't appear to do anything (on MSW, anyway)!
            layout.addWidget( widget )
       
        widget = QWidget()
        widget.setLayout(layout)

        # Set the central widget of the Window to expand it 
        # to take up all the space in the window by default.
        self.setCentralWidget(widget)

#end MainWindow class

def get_darkModePalette( app=None ) :
    
    darkPalette = app.palette()
    darkPalette.setColor( QPalette.Window, QColor( 53, 53, 53 ) )
    darkPalette.setColor( QPalette.WindowText, Qt.white )
    darkPalette.setColor( QPalette.Disabled, QPalette.WindowText, QColor( 127, 127, 127 ) )
    darkPalette.setColor( QPalette.Base, QColor( 42, 42, 42 ) )
    darkPalette.setColor( QPalette.AlternateBase, QColor( 66, 66, 66 ) )
    darkPalette.setColor( QPalette.ToolTipBase, QColor( 53, 53, 53 ) )
    darkPalette.setColor( QPalette.ToolTipText, Qt.white )
    darkPalette.setColor( QPalette.Text, Qt.white )
    darkPalette.setColor( QPalette.Disabled, QPalette.Text, QColor( 127, 127, 127 ) )
    darkPalette.setColor( QPalette.Dark, QColor( 35, 35, 35 ) )
    darkPalette.setColor( QPalette.Shadow, QColor( 20, 20, 20 ) )
    darkPalette.setColor( QPalette.Button, QColor( 53, 53, 53 ) )
    darkPalette.setColor( QPalette.ButtonText, Qt.white )
    darkPalette.setColor( QPalette.Disabled, QPalette.ButtonText, QColor( 127, 127, 127 ) )
    darkPalette.setColor( QPalette.BrightText, Qt.red )
    darkPalette.setColor( QPalette.Link, QColor( 42, 130, 218 ) )
    darkPalette.setColor( QPalette.Highlight, QColor( 42, 130, 218 ) )
    darkPalette.setColor( QPalette.Disabled, QPalette.Highlight, QColor( 80, 80, 80 ) )
    darkPalette.setColor( QPalette.HighlightedText, Qt.white )
    darkPalette.setColor( QPalette.Disabled, QPalette.HighlightedText, QColor( 127, 127, 127 ), )
    
    return darkPalette
    
#end get_darkModePalette def

#================================================

app = QApplication( sys.argv + ['-platform', 'windows:darkmode=2'] )
app.setStyle( 'Fusion' )
app.setPalette( get_darkModePalette( app ) )

mainWindow = MainWindow()  # Replace with your custom mainwindow.
mainWindow.setGeometry(500, 100, 300, 625)
mainWindow.show()

sys.exit( app.exec() )
Monitor answered 27/4, 2023 at 17:36 Comment(0)
M
1

The QPalette constructor can create the palette based on the color of the button. Black color doesn't work well in PySide6, so I chose a slightly lighter color.

app = QCoreApplication.instance()
app.setPalette(QPalette(QColor("#444444")))

That is all

Mortie answered 11/2 at 12:13 Comment(0)
J
1
pip install PySide6
def main():
    app.setStyle("Fusion")

if __name__ == "__main__":
    main()

it will automatically detect your systems theme and use it

Jacy answered 10/5 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.