我正在創建C++/QML應用程序。在調試版本中,我想直接從文件加載QML,在發佈版本中我想從資源加載QML。我想用QDir::setSearchPaths
此:QML組件未找到,即使在相同的目錄中
void GuiController::initQmlPath()
{
#ifdef QT_DEBUG
QDir dir(QApplication::applicationDirPath());
const bool success = dir.cd("../../Game/Qml"); // Depends on project structure
Q_ASSERT(success);
QDir::setSearchPaths("qml", QStringList(dir.absolutePath()));
#else
QDir::setSearchPaths("qml", QStringList(":/Game/Qml"));
#endif
}
我加載在明年方式成分:
connect(component, &QQmlComponent::statusChanged, stateSlot);
//component->loadUrl(QUrl("qml:/MainWindow.qml")); // Not working
component->loadUrl(QUrl::fromLocalFile("C:/Projects/Launcher/Game/Qml/MainWindow.qml")); // OK
當我使用loadUrl
完整路徑 - 一切正常,但是當我使用qml:/MainWindow.qml
- 文件被發現,但它不能加載部件,被放置在相同的文件夾(簡化):
MainWindow.qml
Window {
id: root
visible: true
CustomComponent {
}
}
CustomComponent.qml
Rectangle {
id: root
color: "red"
}
如何解決呢? (如何設置在通過搜索路徑QML引擎查找文件夾)
檢查是否'QUrl( 「QML:/MainWindow.qml」)'產生一個有效的URL 。我不確定'QUrl'是否支持搜索路徑。 – vahancho 2014-11-21 10:43:13
發現奇怪的文件,因爲它顯示正確的錯誤(「qml:/MainWindow.qml:28 CustomComponent不是一個類型」) – 2014-11-21 10:53:05