daniele86
daniele86

Reputation: 147

Qml QtQuickControls2, change style properties at runtime

I should set property of Material style at runtime, for example to change theme value (light/dark) when the user click on a defined button. I have configured Material style with qtquickcontrols2.conf and its properties (theme, accent and primary). 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. My goal is simply change theme of material style from light to dark and viceversa on runtime. How can I integrate this feature? Thanks in advice.

Best Regards Daniele

Upvotes: 2

Views: 4385

Answers (1)

Mitch
Mitch

Reputation: 24416

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.

You need at least Qt 5.7.0 in order to have the Qt Quick Controls 2.0 import available.

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

You switch the theme at runtime like this:

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
    }
}

gif

Upvotes: 7

Related Questions