와 함께 파일 선택기 사용 Qt Quick Controls
File selectors 는 파일 변형을 선택하는 편리한 방법을 제공합니다. Qt는 플랫폼 이름과 로케일을 내장 셀렉터로 제공합니다. Qt Quick Controls 는 애플리케이션이 실행 중인 스타일의 이름(대문자)으로 내장 셀렉터를 확장합니다.
파일 선택기를 사용하면 스타일에 대한 하드 종속성을 만들지 않고 스타일별 조정을 적용할 수 있습니다. 사용 가능한 파일 변형 중에서 선택한 QML 파일만 QML 엔진에 의해 로드됩니다. 각 파일 변형은 컨텍스트, 즉 특정 스타일을 가정할 수 있습니다. 이는 일반적으로 약간의 코드 중복으로 이어지지만, 다른 한편으로는 앞서 언급한 스타일에 대한 종속성을 줄이고 더 간단하고 효율적인 QML 코드를 만들 수 있습니다.
다음 예시는 머티리얼 스타일에서는 스타일이 적용된 그림자가 있고 다른 스타일에서는 평평하게 보이는 사용자 지정 둥근 버튼을 보여줍니다. 파일은 CustomButton.qml
의 머티리얼 버전이 +Material
하위 디렉터리에 배치되도록 구성되어 있습니다.
:/main.qml :/CustomButton.qml :/+Material/CustomButton.qml
기본적으로 main.qml
은 CustomButton
유형에 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/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.