自從我將 Qt 升級到6.2 版本以來,我一直遇到這個問題。如果我在5.15 版本中創建一個專案(如圖片中的專案“ untitled1 ”),我可以創建一個新的 QML 組件(“ Meutexto.qml ”檔案)并在主檔案中呼叫它。但是,如果我在 6.2 版本的專案(“無標題”)中創建相同的東西,我得到了錯誤:qrc:/untitled/main.qml:9:5: Meutexto is not a type.
6.2 版本自動創建 QML 專案檔案夾,5.1??5 版本使用 Resources 專案檔案夾來存盤 QML 檔案。這個問題可能與此有關嗎?
更多細節:
我對下面圖片和代碼中的示例有疑問。我在 Qt 6.2 中遇到了這個錯誤:QQmlApplicationEngine failed to load component qrc:/untitled/main.qml:9:5: Meutexto is not a type。我在 5.15 版本中沒有收到該錯誤。


main.qml代碼:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Meutexto {}
}
Meutexto.qml代碼:
import QtQuick 2.0
Item {
Text {
anchors.centerIn: parent
text: "My Text"
}
}
untitle.pro的代碼:
QT = quick
SOURCES = \
main.cpp
resources.files = main.qml
resources.prefix = /$${TARGET}
RESOURCES = resources \
qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS = target
DISTFILES = \
Meutexto.qml
qml.qrc代碼:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>Meutexto.qml</file>
</qresource>
</RCC>
uj5u.com熱心網友回復:
您沒有提供 main.cpp 檔案,但我創建了一個新專案并注意到它生成的默認專案使用如下所示的 URL:
const QUrl url(u"qrc:/my_project/main.qml"_qs);
把它改成這樣對我來說更有意義:
const QUrl url(u"qrc:/main.qml"_qs);
更改后,有兩種方法可以使您的代碼正常作業:
- 使用與在 Qt 5.15 中相同的方式使用 qml.qrc 檔案。隨著 main.cpp 的更改,您不需要對 .qrc 檔案進行任何更改。
無標題.pro:
RESOURCES = qml.qrc
- 另一種方法似乎是一種從 .pro 檔案中自動創建資源檔案的新方法。我對這種方法不太熟悉,但它記錄在這里。在您的 .pro 檔案中,執行以下操作:
resources.files = \
main.qml \
Meutexto.qml
resources.prefix = /
RESOURCES = resources
這看起來更好一些,因為您不再需要手動創建 .qrc 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489345.html
