Qt 小部件的深色主题? [英] Dark theme for Qt widgets?

查看:64
本文介绍了Qt 小部件的深色主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在构建一个 PyQt5 应用程序,我想要一个深色主题.以前我曾从事 Android 开发工作,其中有一个我可以为整个应用程序设置的黑暗主题

问题

Qt 中是否内置了深色主题(适用于应用程序中的所有小部件,并且是跨平台的)?

解决方案

不,但你可以使用我相当全面的

动态样式表切换

响应评论,调整样式表以动态使用浅色或深色样式表的最简单方法是将其包装在一个函数中.然后,您可以将该函数用作 Qt 信号的槽(警告:我主要使用 C++ 进行开发,因此我的信号/槽机制代码中可能存在小错误).

def toggle_stylesheet(path):'''切换样式表以使用 Qt 资源中的所需路径系统(以`:/` 为前缀)或一般(文件的路径)系统).:path: 系统上资源或文件的完整路径'''# 获取 QApplication 实例,如果未设置则崩溃app = QApplication.instance()如果应用程序是无:raise RuntimeError("未找到 Qt 应用程序.")文件 = QFile(路径)file.open(QFile.ReadOnly | QFile.Text)流 = QTextStream(文件)app.setStyleSheet(stream.readAll())

现在我们可以添加可以在信号/槽机制中使用此函数的通用应用程序逻辑(如果需要,使用 lambda 作为方便的包装器,以提供样式表切换器的路径):

# 添加应用设置逻辑app = QApplication(sys.argv)# 用于创建顶级小部件的更多逻辑、应用程序逻辑...父母 = ...light_btn = QPushButton("切换灯光.", parent)light_btn.clicked.connect(lambda: toggle_stylesheet(":/light.qss"))dark_btn = QPushButton("切换黑暗.", parent)dark_btn.clicked.connect(lambda: toggle_stylesheet(":/dark.qss"))# 添加到布局中,做其他事情# ...# 结束 Qt 应用程序sys.exit(app.exec_())

这允许用户将使用 PyQt5(或使用 C++、Qt5 中的类似逻辑)开发的应用程序的主题动态更改为浅色或深色主题.

免责声明:显然我是维护者.

Background

I'm building a PyQt5 application, that I'd like to have a dark theme for. Previously I've worked with Android development where there was a dark theme that I could set for a whole application

Question

Is there a dark theme built into Qt (that applies to all widgets in an application, and that is cross-platform)?

解决方案

No, but you may use my fairly comprehensive stylesheets that should look excellent on most platforms (it's inspired by KDE's Breeze Theme, which is a dark theme that is quite elegant). This was (hard) forked from the excellent QDarkStylesheet, which I felt had theme issues in numerous areas, so I modified it extensively for my own needs and added a light theme.

Simple Use

A sample of the theme is here. To use it in PyQt5, simply add the following lines to a project:

import sys
from PyQt5.QtCore import QFile, QTextStream
from PyQt5.QtWidgets import QApplication
import breeze_resources

app = QApplication(sys.argv)
file = QFile(":/dark.qss")
file.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(file)
app.setStyleSheet(stream.readAll())

Dynamic Stylesheet Toggling

In response to a comment, the easiest way to adjust the stylesheet to use either the light or the dark stylesheet dynamically is to wrap it in a function. You may then use the function as a slot to a Qt signal (warning: I primarily develop using C++, so there may be small errors in my code for the signal/slot mechanism).

def toggle_stylesheet(path):
    '''
    Toggle the stylesheet to use the desired path in the Qt resource
    system (prefixed by `:/`) or generically (a path to a file on
    system).

    :path:      A full path to a resource or file on system
    '''

    # get the QApplication instance,  or crash if not set
    app = QApplication.instance()
    if app is None:
        raise RuntimeError("No Qt Application found.")

    file = QFile(path)
    file.open(QFile.ReadOnly | QFile.Text)
    stream = QTextStream(file)
    app.setStyleSheet(stream.readAll())

Now we can add generic application logic that can use this function in a signal/slot mechanism (using a lambda as a convenient wrapper, if needed, to provide the path to the stylesheet toggler):

# add logic for setting up application
app = QApplication(sys.argv)
# more logic for creating top-level widgets, application logic ...

parent = ...
light_btn = QPushButton("Toggle light.", parent)
light_btn.clicked.connect(lambda: toggle_stylesheet(":/light.qss"))

dark_btn = QPushButton("Toggle dark.", parent)
dark_btn.clicked.connect(lambda: toggle_stylesheet(":/dark.qss"))

# add to the layout, do other stuff
# ...

# end the Qt application
sys.exit(app.exec_())

This allows users to dynamically change the theme of an application developed with PyQt5 (or using analogous logic in C++, Qt5) to either a light or dark theme.

Disclaimer: Obviously I am the maintainer.

这篇关于Qt 小部件的深色主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆