如何在Qt QML(QtQuick 2)中设置Material Dark主题? [英] How to set Material Dark Theme in Qt QML (QtQuick 2)?

查看:128
本文介绍了如何在Qt QML(QtQuick 2)中设置Material Dark主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在QtQuick2中为我的应用程序设置Material Dark Theme.我关注了这个官方文档:

现在我要应用深色主题,该主题未在文档中说明.我试图将字符串从材料"更改为到材料.深色"和类似的描述,但没有用.我也找不到"setTheme"函数或类似的QQuickStyle对象,这让我有点头绪.

有人可以提示我如何将深色主题应用于材质样式吗?

解决方案

一种可行的解决方案是使用环境变量 QT_QUICK_CONTROLS_MATERIAL_THEME (请参见

您还可以使用qtquickcontrols2.conf文件(请参见 https://doc.qt.io/qt-5/qtquickcontrols2-configuration.html )

qtquickcontrols2.conf

  [控件]样式=材质[材料]主题=黑暗 

I want to set Material Dark Theme for my application in QtQuick2. I followed this official docs:

https://doc.qt.io/qt-5/qtquickcontrols2-styles.html

And applied one line in my main.cpp (changed nothing else from auto-generated code):

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickStyle>

#include <QDebug>

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);


    //set GUI style theme here
    QQuickStyle::setStyle("Material");




    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

Which will add the default material theme successfully, which looks as expected: Material default Theme:

And now I want to apply the dark theme, which is not stated in the docs. I tried to change the String from "Material" to "Material.Dark" and similiar descriptions, which did not work. I also can't find a "setTheme" function or similiar on the QQuickStyle Object, which makes me a bit clueless.

Can somebody give me a hint on how to apply the dark theme to material style?

解决方案

A possible solution is to use the environment variable QT_QUICK_CONTROLS_MATERIAL_THEME (See https://doc.qt.io/qt-5/qtquickcontrols2-environment.html):

qputenv("QT_QUICK_CONTROLS_STYLE", QByteArray("Material"));
qputenv("QT_QUICK_CONTROLS_MATERIAL_THEME", QByteArray("Dark"));

QGuiApplication app(argc, argv);

You can also use the qtquickcontrols2.conf file(See https://doc.qt.io/qt-5/qtquickcontrols2-configuration.html)

qtquickcontrols2.conf

[Controls]
Style=Material

[Material]
Theme=Dark

这篇关于如何在Qt QML(QtQuick 2)中设置Material Dark主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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