でファイルセレクタを使うQt Quick Controls

File selectors を使うと、ファイルのバリアントを選択する便利な方法が提供されます。Qt では、プラットフォーム名とロケールを組み込みセレクタとして提供しています。 は、アプリケーションを実行しているスタイルの名前(大文字)で組み込みセレクタを拡張しています。Qt Quick Controls

ファイル セレクタを使用することで、スタイルに強く依存することなく、スタイル固有の微調整を適用できます。利用可能なファイルバリアントから、選択されたQMLファイルのみがQMLエンジンによって読み込まれます。各ファイルバリアントはコンテキスト、つまり特定のスタイルを想定することができます。これは一般的にコードの重複を招きますが、その一方で前述のスタイルへのハードな依存性を削減し、よりシンプルで効率的なQMLコードにつながります。

次の例では、マテリアルのスタイルではドロップ シャドウがスタイル化され、他のスタイルではフラットに見える、丸みを帯びたカスタム ボタンを示しています。ファイルは、CustomButton.qml の Material バージョンが+Material サブディレクトリに置かれるように整理されています。

:/main.qml
:/CustomButton.qml
:/+Material/CustomButton.qml

デフォルトでは、main.qmlCustomButton のタイプにCustomButton.qml を使用します。しかし、アプリケーションをマテリアル・スタイルで実行すると、Material セレクタが存在し、代わりに+Material/CustomButton.qml バージョンが使用されます。

// main.qml
import QtQuick
import QtQuick.Controls

ApplicationWindow {
    id: window
    visible: true

    CustomButton {
        text: "Button"
        anchors.centerIn: parent
    }
}

カスタム ボタンの基本実装は、単純な丸みを帯びたフラット ボタンです。

// CustomButton.qml
import QtQuick
import QtQuick.Controls

Button {
    id: control

    background: Rectangle {
        radius: width / 2
        implicitWidth: 36
        implicitHeight: 36
        color: control.pressed ? "#ccc" : "#eee"
    }
}

カスタム ボタンの Material スタイルの実装では、Material スタイルをインポートし、ダーク テーマを要求して明るいテキストを取得し、背景にドロップ シャドウを作成します。

// +Material/CustomButton.qml
import QtQuick
import QtQuick.Effects
import QtQuick.Controls
import QtQuick.Controls.Material

Button {
    id: control

    Material.theme: Material.Dark

    background: Rectangle {
        implicitWidth: 48
        implicitHeight: 48
        color: Material.accentColor
        radius: width / 2

        layer.enabled: control.enabled
        layer.effect: MultiEffect {
            shadowEnabled: true
            shadowHorizontalOffset: 3
            shadowVerticalOffset: 3
            shadowColor: Material.dropShadowColor
            shadowBlur: control.pressed ? 0.8 : 0.4
        }
    }
}

注: 内部でQQmlFileSelector インスタンスを作成するQQmlApplicationEngine を使用することをお勧めします。これだけでQMLファイルセレクタを使用することができます。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.