Qml QtQuickControls2,在运行时更改样式属性

Qml QtQuickControls2, change style properties at runtime

我应该在运行时设置 属性 或 Material 样式,例如当用户单击定义的按钮时更改主题值 (light/dark)。 我已经使用 qtquickcontrols2.conf 及其属性(主题、重音和主要)配置了 Material 样式。我无法导入 QtQuick.Controls.Materials 2.0,因为我不知道,但我正在使用 QtCreator 4.0.2 处理 Ubuntu 并且 QtQuick.Controls.Materials 和 QtQuick.Controls.Universal 导入不是检测到。 我的目标只是在运行时将 material 样式的主题从浅色更改为深色,反之亦然。如何集成此功能?谢谢指教。

最好的问候 丹妮尔

I can't import QtQuick.Controls.Materials 2.0 because I don't know, but I'm working on Ubuntu with QtCreator 4.0.2 and the QtQuick.Controls.Materials and QtQuick.Controls.Universal imports are not detected.

您至少需要 Qt 5.7.0 才能使用 Qt Quick Controls 2.0 导入。

My goal is simply change theme of material style from light to dark and viceversa on runtime.

您可以像这样在运行时切换主题:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0

ApplicationWindow {
    id: window
    width: 200
    height: 200
    visible: true

    Material.theme: themeSwitch.checked ? Material.Dark : Material.Light

    Switch {
        id: themeSwitch
        text: "Dark"
        anchors.centerIn: parent
    }
}