Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problems creating dark palette



  • I am using PySide2, with Qt 5.15.2 in side of Unreal Engine. I would like to create a look that matches Unreal. I have seen lots of chatter on forums about creating a dark style/theme but can't find anything directly useful.

    I thought I would try and create my own. Unfortunately there are some things I don't seem to be able to set the color for.

    I built this test case. I tried to set everything to red. I think I should get a completely red window but instead I see this:

    e9cf5e77-0a5c-4918-af11-70418d7a354c-image.png

    Here is my code:

    import sys

    from PySide2 import QtCore, QtGui, QtUiTools, QtWidgets
    from PySide2.QtGui import QPalette, QColor
    from PySide2.QtCore import Qt

    def test():

    app = QtWidgets.QApplication(sys.argv)
    
    palette = QPalette()
    
    for groupName, group in QPalette.ColorGroup.values.items():
        for roleName, role in QPalette.ColorRole.values.items():
            print(f'{groupName} - {roleName}, {int(role)}')
            if role == QPalette.ColorRole.NColorRoles:
                continue
            palette.setColor(group, role, Qt.red)
    
    app.setPalette(palette)
    
    dlg = QtWidgets.QDialog()
    button = QtWidgets.QPushButton("test", parent=dlg)
    dlg.show()
    app.exec_()
    

    test()

    Why is the button background not red? What am I doing wrong?

    Thanks,
    Chris


  • Moderators

    Hi @Chris-King, and welcome!

    I'm note sure why, but it seems like not all roles got set.

    The QPalette.ButtonText role seems to have been updated correctly; try to manually set the QPalette.Button role, which determines the button background.



  • I tried that but it didn't work. I started out explicitly setting all the roles that I found in the documentation but there seem to be a couple of things that just don't get set that way. There is the button color and the shadow around the button that is a blue color.

    Is there something that can be overriding the palette?

    I really wish Qt had a built in dark theme or better yet a collection of themes that shipped with the source that developers could choose from.


  • Lifetime Qt Champion

    @Chris-King

    Hi
    The look of Qt is drawn via QStyle and not all platform uses the same palette for the same elements.
    Also, other elements like the blue active frame might not use the palette at all.
    So it's not possible to make a complete dark style using only the palette as far as I found.

    You can get far with stylesheets.
    https://github.com/ColinDuquesnoy/QDarkStyleSheet

    • I really wish Qt had a built in dark theme or better yet a collection of themes that shipped with the source that developers could choose from.

    Well same here. On linux , a lot of themes exists and if using the right Desktop manager, you can style it to a huge degree.
    On windows, you can barely set the color as you wish and even it has a dark mode, it's not easy to reuse for a Qt app.
    It might come someday

    https://bugreports.qt.io/browse/QTBUG-72028


  • Moderators

    @mrjj said in Problems creating dark palette:

    The look of Qt is drawn via QStyle and not all platform uses the same palette for the same elements.

    Ah, that's right; I forgot about that.

    If you still want to use palettes, the Fusion style has good support for palettes. Here's an example that sets a dark theme with Fusion (buttons definitely work): https://forum.qt.io/topic/25278/solved-fusion-style-how-to-set-dark-version


Log in to reply