QML 从子目录导入样式文件
QML import style file from subdirectory
我在从子目录导入样式文件时遇到问题。
这是我的 *.pro 文件中的相关部分:
QML_IMPORT_PATH = \
src/gui/qml/views/startview/ \
src/gui/qml/views/createlocalgameview/ \
src/gui/qml/views/ \
src/gui/qml/components/styles/ \
src/gui/qml/components/ \
src/gui/qml/js/
例如我有一个文件 MyTextFieldStyle.qml 放在 src/gui/qml/components/styles/:
TextFieldStyle {
[...]
}
我有一个文件 TextArea.qml 放在 src/gui/qml/components/:
Item {
[...]
TextField {
id: textField
style: MyTextFieldStyle {}
}
[...]
}
现在我得到这个错误:
TextArea.qml: MyTextFieldStyle is not a type
如果我将 MyTextFieldStyle.qml 放在与 TextArea.qml (src/gui/qml/components/) 相同的目录中,它可以正常工作。但是我有很多组件和很多样式,所以我想将它们分开以便更好地了解。有什么方法可以让它正常工作吗?
在文件顶部放置一个带有相对路径的 import
语句就足够了。
在您的示例中,您可以使用 import './styles'
。
如评论中所述,QML_IMPORT_PATH
并不是要解决该问题。
我在从子目录导入样式文件时遇到问题。
这是我的 *.pro 文件中的相关部分:
QML_IMPORT_PATH = \
src/gui/qml/views/startview/ \
src/gui/qml/views/createlocalgameview/ \
src/gui/qml/views/ \
src/gui/qml/components/styles/ \
src/gui/qml/components/ \
src/gui/qml/js/
例如我有一个文件 MyTextFieldStyle.qml 放在 src/gui/qml/components/styles/:
TextFieldStyle {
[...]
}
我有一个文件 TextArea.qml 放在 src/gui/qml/components/:
Item {
[...]
TextField {
id: textField
style: MyTextFieldStyle {}
}
[...]
}
现在我得到这个错误:
TextArea.qml: MyTextFieldStyle is not a type
如果我将 MyTextFieldStyle.qml 放在与 TextArea.qml (src/gui/qml/components/) 相同的目录中,它可以正常工作。但是我有很多组件和很多样式,所以我想将它们分开以便更好地了解。有什么方法可以让它正常工作吗?
在文件顶部放置一个带有相对路径的 import
语句就足够了。
在您的示例中,您可以使用 import './styles'
。
如评论中所述,QML_IMPORT_PATH
并不是要解决该问题。