2014-11-21 78 views
0

我正在创建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引擎查找文件夹)

+1

检查是否'QUrl( “QML:/MainWindow.qml”)'产生一个有效的URL 。我不确定'QUrl'是否支持搜索路径。 – vahancho 2014-11-21 10:43:13

+0

发现奇怪的文件,因为它显示正确的错误(“qml:/MainWindow.qml:28 CustomComponent不是一个类型”) – 2014-11-21 10:53:05

回答

0

在接下来的方式使URL解决:

QUrl::fromLocalFile(QFileInfo("qml:/MainWindow.qml").absoluteFilePath()); 
+0

如果您回答了您自己的问题,请将答案标记为已接受,以表明它解决了您的问题。 – tonytony 2014-11-29 11:09:18

+0

当然。这是不可能的。 – 2014-11-30 11:32:11