以下專案結構適用于匯入 qml 模塊:
專案結構
Project
|- modules
|- Goofy
|- qmldir
|- Donald.qml
|- project.pro
|- main.cpp
|- main.qml
|- qml.qrc
專案.pro
QT = quick
SOURCES = \
main.cpp
RESOURCES = \
modules/Goofy/Donald.qml \
modules/Goofy/qmldir \
qml.qrc
QML_IMPORT_PATH = $$PWD/modules
QML_DESIGNER_IMPORT_PATH = $$PWD/modules
主檔案
QQmlApplicationEngine engine;
engine.addImportPath("qrc:/modules");
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import Goofy 1.0
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Donald{}
}
qmldir
module Goofy
Donald 1.0 Donald.qml
我應該如何修改我的專案以從 qrc 檔案中匯入模塊,而不是向資源中添加任何單個檔案?
理想情況下,我想要以下專業檔案:
專案.pro
QT = quick
SOURCES = \
main.cpp
RESOURCES = \
modules/Goofy/goofy.qrc \
qml.qrc
QML_IMPORT_PATH = $$PWD/modules
QML_DESIGNER_IMPORT_PATH = $$PWD/modules
我嘗試使用以下格式將 goofy.qrc(或 Goofy.qrc)添加到我的 modules/Goofy 檔案夾中:
<RCC>
<qresource prefix="/">
<file>qmldir</file>
<file>Donald.qml</file>
</qresource>
</RCC>
但它不起作用。我該怎么辦?
uj5u.com熱心網友回復:
您的 Goofy.rcc 不起作用,因為檔案不在使用的 importPath 中。這些檔案位于 qrc 旁邊,因此沒有將相對路徑添加到指定的前綴。將 rcc 更改為以下內容以使其作業:
<RCC>
<qresource prefix="/modules/Goofy">
<file>qmldir</file>
<file>Donald.qml</file>
</qresource>
</RCC>
正如您似乎已經理解的那樣,qmldir 檔案需要Goofy位于 QmlEngine 接受它的路徑中。將其添加到指定的匯入路徑,QmlEngine 將找到您的模塊,就像它在磁盤上一樣(因為 qrc 實際上被呈現為普通檔案系統)
順便說一句,您可以使用以下代碼段檢查相對 qrc 路徑的作業情況:
QDirIterator qrc(":", QDirIterator::Subdirectories);
while(qrc.hasNext())
{
qDebug() << qrc.next();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418536.html
標籤:
上一篇:對QML組件的無效別名參考
下一篇:自定義WordWrapQt.TextWrapAnywherePyQt5之后的QLabelsetMinimumHeight(帶/不帶表情符號的完全回應式)
