Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt小部件的黑暗主题? 背景_Qt_Pyqt_Qt5_Pyqt5_Qstyle - Fatal编程技术网

Qt小部件的黑暗主题? 背景

Qt小部件的黑暗主题? 背景,qt,pyqt,qt5,pyqt5,qstyle,Qt,Pyqt,Qt5,Pyqt5,Qstyle,我正在构建一个PyQt5应用程序,我希望它有一个黑暗的主题。之前,我在Android开发中工作过,我可以为整个应用程序设置一个黑暗的主题 问题: Qt中是否内置了一个黑暗主题(适用于应用程序中的所有小部件,并且是跨平台的)?没有,但您可以使用我的相当全面的主题,它在大多数平台上看起来都非常出色(它的灵感来自KDE的Breeze主题,这是一个非常优雅的黑暗主题)。这是(硬的)从优秀中派生出来的,我觉得它在许多领域都有主题问题,所以我根据自己的需要对它进行了广泛的修改,并添加了一个轻松的主题 简单使

我正在构建一个PyQt5应用程序,我希望它有一个黑暗的主题。之前,我在Android开发中工作过,我可以为整个应用程序设置一个黑暗的主题

问题:
Qt中是否内置了一个黑暗主题(适用于应用程序中的所有小部件,并且是跨平台的)?

没有,但您可以使用我的相当全面的主题,它在大多数平台上看起来都非常出色(它的灵感来自KDE的Breeze主题,这是一个非常优雅的黑暗主题)。这是(硬的)从优秀中派生出来的,我觉得它在许多领域都有主题问题,所以我根据自己的需要对它进行了广泛的修改,并添加了一个轻松的主题

简单使用 这里有一个主题示例。要在PyQt5中使用它,只需将以下行添加到项目中:

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())

动态样式表切换 为了响应注释,动态调整样式表以使用亮样式表或暗样式表的最简单方法是将其包装在函数中。然后,您可以使用该函数作为QT信号的时隙(警告:我主要使用C++开发,因此在我的代码中可能存在信号/时隙机制的小错误)。 现在,我们可以添加可以在信号/插槽机制中使用此函数的通用应用程序逻辑(如果需要,可以使用lambda作为方便的包装器来提供样式表切换器的路径):

<>这允许用户动态地改变用PyQT5开发的应用程序的主题(或者使用C++中的类似逻辑,QT5)到一个亮或暗的主题。
免责声明:显然我是维护者。

建立在我的书签中。我不记得最初的来源了

QApplication::setStyle(QStyleFactory::create("Fusion"));
QPalette p;
p = qApp->palette();
p.setColor(QPalette::Window, QColor(53,53,53));
p.setColor(QPalette::Button, QColor(53,53,53));
p.setColor(QPalette::Highlight, QColor(142,45,197));
p.setColor(QPalette::ButtonText, QColor(255,255,255));
qApp->setPalette(p);

另外,如有必要,可以使用QSS进行调整。

Qt中没有内置黑暗主题。但您可以使用以下代码轻松创建一个:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPalette, QColor

app = QApplication([])
# Force the style to be the same on all OSs:
app.setStyle("Fusion")

# Now use a palette to switch to dark colors:
palette = QPalette()
palette.setColor(QPalette.Window, QColor(53, 53, 53))
palette.setColor(QPalette.WindowText, Qt.white)
palette.setColor(QPalette.Base, QColor(25, 25, 25))
palette.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
palette.setColor(QPalette.ToolTipBase, Qt.black)
palette.setColor(QPalette.ToolTipText, Qt.white)
palette.setColor(QPalette.Text, Qt.white)
palette.setColor(QPalette.Button, QColor(53, 53, 53))
palette.setColor(QPalette.ButtonText, Qt.white)
palette.setColor(QPalette.BrightText, Qt.red)
palette.setColor(QPalette.Link, QColor(42, 130, 218))
palette.setColor(QPalette.Highlight, QColor(42, 130, 218))
palette.setColor(QPalette.HighlightedText, Qt.black)
app.setPalette(palette)
这样做的好处是它不引入外部依赖项。如果您对上述更改感兴趣,我创建了一个示例。以下是一个屏幕截图:


QT Creator提供黑暗主题。在Qt Creator应用程序中启用它 转到工具
->选项->文本编辑器->选择主题为暗


我试图将此应用于我的应用程序,发现下面的内容很容易让我通过将其应用于AppContext来设置应用程序的样式

class AppContext(ApplicationContext):
    def run(self):
        self.main_window.show()
        return self.app.exec_()

    @cached_property
    def main_window(self):
        return MainWindow(self)

    if theme_selection == 'Dark':
        QApplication.setStyle("Fusion")
        #
        # # Now use a palette to switch to dark colors:
        dark_palette = QPalette()
        dark_palette.setColor(QPalette.Window, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.WindowText, Qt.white)
        dark_palette.setColor(QPalette.Base, QColor(35, 35, 35))
        dark_palette.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.ToolTipBase, QColor(25, 25, 25))
        dark_palette.setColor(QPalette.ToolTipText, Qt.white)
        dark_palette.setColor(QPalette.Text, Qt.white)
        dark_palette.setColor(QPalette.Button, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.ButtonText, Qt.white)
        dark_palette.setColor(QPalette.BrightText, Qt.red)
        dark_palette.setColor(QPalette.Link, QColor(42, 130, 218))
        dark_palette.setColor(QPalette.Highlight, QColor(42, 130, 218))
        dark_palette.setColor(QPalette.HighlightedText, QColor(35, 35, 35))
        dark_palette.setColor(QPalette.Active, QPalette.Button, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.Disabled, QPalette.ButtonText, Qt.darkGray)
        dark_palette.setColor(QPalette.Disabled, QPalette.WindowText, Qt.darkGray)
        dark_palette.setColor(QPalette.Disabled, QPalette.Text, Qt.darkGray)
        dark_palette.setColor(QPalette.Disabled, QPalette.Light, QColor(53, 53, 53))
        QApplication.setPalette(dark_palette)
    elif theme_selection == 'Light':
        QApplication.setStyle("")
        pass
    else:
        pass
您可以使用Qsettings保存类似于此模式的首选项,并在启动时恢复

if settings.contains("theme_selection"):
    # there is the key in QSettings
    print('Checking for theme preference in config')
    theme_selection = settings.value('theme_selection')
    print('Found theme_selection in config:' + theme_selection)
else:
    if not is_mac():
        print('theme_selection not found in config. Using default Darkmode')
        settings.setValue('theme_selection', 'Dark')
        theme_selection = settings.value('theme_selection')
    elif is_mac():
        print('theme_selection not found in config. Using default Lightmode')
        settings.setValue('theme_selection', 'Light')
        theme_selection = settings.value('theme_selection')
    pass
看起来很神奇。我无法评论迈克尔·赫尔曼的帖子说谢谢,但投了赞成票

之前:

之后:


中间部分是xterm.js,这就是为什么它现在仍然是白色的,因为它不是QT样式的东西。

不是。你必须制作自己的调色板或下载一个。检查下面的链接。我尝试过的所有其他黑色主题都缺少部分,看起来不太好,有缺陷,被砍掉了,填充和布局太乱了。这是目前为止最好的。也就是说它并不完美,但可能只需要稍作调整。带有活动停靠复选框的停靠列表的复选框与停靠名称重叠。@leetNightshade请随意发布问题,我可以解决它。功能或拉取请求将非常好。@AlexanderHuszagh是否可以在启用暗主题和禁用暗主题之间切换?似乎在我调用“sys.exit(app.exec_())”之后,我再也无法设置app@aoh让我知道,如果这解决了您的问题,它简单地解释了如何在运行时在明暗样式之间切换,要使用本机样式,您只需调用
app.setStyleSheet(“”)
。将这些绑定到UI中的信号将允许用户在运行时更改样式表,最好将他们首选的UI存储在配置文件中。@AlexanderHuszagh它肯定回答了我的问题,谢谢!OP询问如何更改PyQt应用程序中的样式,而不是如何更改Qt Creator的主题。使用此方法,您将如何返回到标准调色板?更新了我的帖子,以反映我使用QSETING处理明暗模式的当前方式。默认图标如何?这必须单独考虑,对吗?是的,使用深色调色板似乎是解决方案。我所做的:我使用我的macOS,将其更改为黑暗主题,Qt识别出这一点(5.12),并在黑暗主题中渲染所有内容。然后,我从调色板中提取所有颜色,并对它们进行设置,这样我也可以在Windows上使用它们。是的,图标需要单独使用。我有平面/单色的SVG,所以我只运行一个脚本来复制它们,用文本替换颜色,并有一个包装函数,根据主题返回亮/暗图标。顺便说一下,我还需要替换调色板的亮/中/暗/中/阴影才能正常工作
class AppContext(ApplicationContext):
    def run(self):
        self.main_window.show()
        return self.app.exec_()

    @cached_property
    def main_window(self):
        return MainWindow(self)

    if theme_selection == 'Dark':
        QApplication.setStyle("Fusion")
        #
        # # Now use a palette to switch to dark colors:
        dark_palette = QPalette()
        dark_palette.setColor(QPalette.Window, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.WindowText, Qt.white)
        dark_palette.setColor(QPalette.Base, QColor(35, 35, 35))
        dark_palette.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.ToolTipBase, QColor(25, 25, 25))
        dark_palette.setColor(QPalette.ToolTipText, Qt.white)
        dark_palette.setColor(QPalette.Text, Qt.white)
        dark_palette.setColor(QPalette.Button, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.ButtonText, Qt.white)
        dark_palette.setColor(QPalette.BrightText, Qt.red)
        dark_palette.setColor(QPalette.Link, QColor(42, 130, 218))
        dark_palette.setColor(QPalette.Highlight, QColor(42, 130, 218))
        dark_palette.setColor(QPalette.HighlightedText, QColor(35, 35, 35))
        dark_palette.setColor(QPalette.Active, QPalette.Button, QColor(53, 53, 53))
        dark_palette.setColor(QPalette.Disabled, QPalette.ButtonText, Qt.darkGray)
        dark_palette.setColor(QPalette.Disabled, QPalette.WindowText, Qt.darkGray)
        dark_palette.setColor(QPalette.Disabled, QPalette.Text, Qt.darkGray)
        dark_palette.setColor(QPalette.Disabled, QPalette.Light, QColor(53, 53, 53))
        QApplication.setPalette(dark_palette)
    elif theme_selection == 'Light':
        QApplication.setStyle("")
        pass
    else:
        pass
if settings.contains("theme_selection"):
    # there is the key in QSettings
    print('Checking for theme preference in config')
    theme_selection = settings.value('theme_selection')
    print('Found theme_selection in config:' + theme_selection)
else:
    if not is_mac():
        print('theme_selection not found in config. Using default Darkmode')
        settings.setValue('theme_selection', 'Dark')
        theme_selection = settings.value('theme_selection')
    elif is_mac():
        print('theme_selection not found in config. Using default Lightmode')
        settings.setValue('theme_selection', 'Light')
        theme_selection = settings.value('theme_selection')
    pass