IDE 无法识别 QML 模块
QML module not recognized by IDE
我在将 QML 模块导入主文件以便 Qt Design Studio 识别它时遇到问题。
我有以下文件夹结构:
Gui
┣ imports
┃ ┣ delegates
┃ ┃ ┣ ActionBarButton.qml
┃ ┃ ┣ CellBase.qml
┃ ┃ ┣ CellBaseImage.qml
┃ ┃ ┣ NameText.qml
┃ ┃ ┣ qmldir
┃ ┃ ┣ TextIndicator.qml
┃ ┃ ┗ ViewCountIndicator.qml
┃ ┣ searchbar
┃ ┃ ┣ qmldir
┃ ┃ ┗ SearchBar.qml
┣ Main.qml
这是 delegates/qmldir
的样子:
module delegates
TextIndicator 1.0 TextIndicator.qml
这就是 searchbar/qmldir
的样子:
module searchbar
SearchBar 1.0 SearchBar.qml
当我在 main.qml 时,我会:
import delegates 1.0
import searchbar 1.0
两个导入都被 IDE (Qt Design Studio) 接受,如果我 运行 使用 QML_IMPORT_TRACE: "1"
环境变量导入 trance 我得到:
qt.qml.import: addLibraryImport: file:///Gui/Main.qml "delegates" 1.0 as ""
qt.qml.import: importExtension: file:///Gui/Main.qml loaded "Gui/imports/delegates/qmldir"
qt.qml.import: addLibraryImport: file:///Gui/Main.qml "searchbar" 1.0 as ""
qt.qml.import: importExtension: file:///Gui/Main.qml loaded "Gui/imports/searchbar/qmldir"
据我所知,这意味着模块已成功加载。然而,当我在 Main.qml
中尝试访问 TextIndicator
(位于委托模块中)时,IDE 不知道我在说什么。(不会自动完成类型。当我手动输入时,类型。它看不到内部属性或将其突出显示为类型。它对锚点或宽度和高度等常见属性没有帮助。)另一方面,当尝试访问 'SearchBar'(位于搜索栏模块中,它工作正常。)
我正在努力弄清楚为什么它对前者不起作用以及两者之间有什么区别。
这是它在编辑器中的样子:
然而,当我 运行 这个例子时,搜索栏和文本指示器都出现在场景中:
有谁知道如何让 IDE 知道 delegates
模块?
这是 TextIndicator.qml 里面的样子:
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
id: durationIndicator
property string indicatorText: "hello"
property color backgroundColor: "black"
property color textColor: "white"
property int fontPointSize: 15
width: durationText.width
height: durationText.height
Text {
id: durationText
text: indicatorText
anchors.centerIn: parent
color: textColor
font.pointSize: fontPointSize
z: 1
}
Rectangle {
id: durationIndcatorBackground
anchors.fill: durationText
anchors.leftMargin: -2
anchors.rightMargin: -2
anchors.bottomMargin: -2
color: backgroundColor
z: 0
}
}
这是SearchBar.qml:
import QtQuick 2.15
import QtQuick.Controls 2.15
Rectangle {
id: searchBarRoot
width: 1000
height: 80
color: "grey"
signal acceptedText(string searchText, string searchInText, string orderByText, bool excludedFromWatched, bool isDesc)
Row {
id: itemRow
anchors.fill: parent
anchors.verticalCenter: parent.verticalCenter
anchors.margins: 10
spacing: 10
TextField {
id: textInput
// anchors.left: parent.left
// anchors.leftMargin: 80
// anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
width: 300
// height: 20
text: "searchtext"
font.pixelSize: 12
focus: true
onFocusChanged: {
}
onAccepted: {
console.log("Accepted text: " + textInput.text)
searchBarRoot.acceptedText(textInput.text,
searchFieldComboBox.text,
orderByCombobox.currentText,
isWatched.checked,
isDescending.checked, isFav.checked)
}
}
ComboBox {
id: searchFieldComboBox
anchors.verticalCenter: parent.verticalCenter
model: ["Path", "Parent Dir"]
}
ComboBox {
id: orderByCombobox
anchors.verticalCenter: parent.verticalCenter
model: ["Path", "Random", "Name", "Rating", "Favorite", "Path To Dir", "Width", "Height", "Duration", "Size", "Codec_Name", "Frame Rate", "Bit_Rate", "Probe_Score", "Date_Added", "Play_count", "Play_time"]
}
CheckBox {
id: isDescending
anchors.verticalCenter: parent.verticalCenter
text: "Is Desc"
}
CheckBox {
id: isWatched
anchors.verticalCenter: parent.verticalCenter
text: "Exclude Watched"
}
CheckBox {
id: isFav
anchors.verticalCenter: parent.verticalCenter
text: "Is Fav"
}
}
Component.onCompleted: {
textInput.forceActiveFocus()
textInput.selectAll()
}
}
尝试按照此处所述设置 QML_IMPORT_PATH:
https://doc.qt.io/qtcreator/creator-qml-modules-with-plugins.html#importing-qml-modules
编辑:您也可以尝试选择工具 -> QML/JS -> 重置代码模型。有时不会自动更新。
这有点尴尬,但重启Qt Design Studio
几次后,似乎问题就解决了。尽管每次将 'delegates' 模块导入另一个文件时我仍然需要重新启动它。否则,它不会意识到这一点。这很奇怪。
我在将 QML 模块导入主文件以便 Qt Design Studio 识别它时遇到问题。
我有以下文件夹结构:
Gui
┣ imports
┃ ┣ delegates
┃ ┃ ┣ ActionBarButton.qml
┃ ┃ ┣ CellBase.qml
┃ ┃ ┣ CellBaseImage.qml
┃ ┃ ┣ NameText.qml
┃ ┃ ┣ qmldir
┃ ┃ ┣ TextIndicator.qml
┃ ┃ ┗ ViewCountIndicator.qml
┃ ┣ searchbar
┃ ┃ ┣ qmldir
┃ ┃ ┗ SearchBar.qml
┣ Main.qml
这是 delegates/qmldir
的样子:
module delegates
TextIndicator 1.0 TextIndicator.qml
这就是 searchbar/qmldir
的样子:
module searchbar
SearchBar 1.0 SearchBar.qml
当我在 main.qml 时,我会:
import delegates 1.0
import searchbar 1.0
两个导入都被 IDE (Qt Design Studio) 接受,如果我 运行 使用 QML_IMPORT_TRACE: "1"
环境变量导入 trance 我得到:
qt.qml.import: addLibraryImport: file:///Gui/Main.qml "delegates" 1.0 as ""
qt.qml.import: importExtension: file:///Gui/Main.qml loaded "Gui/imports/delegates/qmldir"
qt.qml.import: addLibraryImport: file:///Gui/Main.qml "searchbar" 1.0 as ""
qt.qml.import: importExtension: file:///Gui/Main.qml loaded "Gui/imports/searchbar/qmldir"
据我所知,这意味着模块已成功加载。然而,当我在 Main.qml
中尝试访问 TextIndicator
(位于委托模块中)时,IDE 不知道我在说什么。(不会自动完成类型。当我手动输入时,类型。它看不到内部属性或将其突出显示为类型。它对锚点或宽度和高度等常见属性没有帮助。)另一方面,当尝试访问 'SearchBar'(位于搜索栏模块中,它工作正常。)
我正在努力弄清楚为什么它对前者不起作用以及两者之间有什么区别。
这是它在编辑器中的样子:
然而,当我 运行 这个例子时,搜索栏和文本指示器都出现在场景中:
有谁知道如何让 IDE 知道 delegates
模块?
这是 TextIndicator.qml 里面的样子:
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
id: durationIndicator
property string indicatorText: "hello"
property color backgroundColor: "black"
property color textColor: "white"
property int fontPointSize: 15
width: durationText.width
height: durationText.height
Text {
id: durationText
text: indicatorText
anchors.centerIn: parent
color: textColor
font.pointSize: fontPointSize
z: 1
}
Rectangle {
id: durationIndcatorBackground
anchors.fill: durationText
anchors.leftMargin: -2
anchors.rightMargin: -2
anchors.bottomMargin: -2
color: backgroundColor
z: 0
}
}
这是SearchBar.qml:
import QtQuick 2.15
import QtQuick.Controls 2.15
Rectangle {
id: searchBarRoot
width: 1000
height: 80
color: "grey"
signal acceptedText(string searchText, string searchInText, string orderByText, bool excludedFromWatched, bool isDesc)
Row {
id: itemRow
anchors.fill: parent
anchors.verticalCenter: parent.verticalCenter
anchors.margins: 10
spacing: 10
TextField {
id: textInput
// anchors.left: parent.left
// anchors.leftMargin: 80
// anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
width: 300
// height: 20
text: "searchtext"
font.pixelSize: 12
focus: true
onFocusChanged: {
}
onAccepted: {
console.log("Accepted text: " + textInput.text)
searchBarRoot.acceptedText(textInput.text,
searchFieldComboBox.text,
orderByCombobox.currentText,
isWatched.checked,
isDescending.checked, isFav.checked)
}
}
ComboBox {
id: searchFieldComboBox
anchors.verticalCenter: parent.verticalCenter
model: ["Path", "Parent Dir"]
}
ComboBox {
id: orderByCombobox
anchors.verticalCenter: parent.verticalCenter
model: ["Path", "Random", "Name", "Rating", "Favorite", "Path To Dir", "Width", "Height", "Duration", "Size", "Codec_Name", "Frame Rate", "Bit_Rate", "Probe_Score", "Date_Added", "Play_count", "Play_time"]
}
CheckBox {
id: isDescending
anchors.verticalCenter: parent.verticalCenter
text: "Is Desc"
}
CheckBox {
id: isWatched
anchors.verticalCenter: parent.verticalCenter
text: "Exclude Watched"
}
CheckBox {
id: isFav
anchors.verticalCenter: parent.verticalCenter
text: "Is Fav"
}
}
Component.onCompleted: {
textInput.forceActiveFocus()
textInput.selectAll()
}
}
尝试按照此处所述设置 QML_IMPORT_PATH:
https://doc.qt.io/qtcreator/creator-qml-modules-with-plugins.html#importing-qml-modules
编辑:您也可以尝试选择工具 -> QML/JS -> 重置代码模型。有时不会自动更新。
这有点尴尬,但重启Qt Design Studio
几次后,似乎问题就解决了。尽管每次将 'delegates' 模块导入另一个文件时我仍然需要重新启动它。否则,它不会意识到这一点。这很奇怪。