2014-11-21 35 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