Switch(QtQuick Controls 2)サンプル


Switch(QtQuick Controls 2)のサンプル

テスト用QML

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtMultimedia 5.9

ApplicationWindow {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("Switchサンプル")

    Audio { id: clickSound1; source: "Sounds/btn01.mp3" }
    Audio { id: clickSound2; source: "Sounds/btn02.mp3" }

    Column {
        spacing: 2

        Rectangle { id: colorBox; width: root.width; height: root.height / 2; color: "red" }

        // Switch生成
        Switch {
            id: sw
            width: root.width
            height: root.height / 2
            onToggled: {
                if (checked == true) {      // 切り替えで colorBox の色を変える
                    clickSound1.play()
                    colorBox.color = "blue"
                } else {
                    clickSound2.play()
                    colorBox.color = "red"
                }
            }
        }
    }
}

実行結果

SignalTransitionサンプル動画
※Linux Mint 18.2 & Qt 5.9.1使用