我正在创建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