Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

QML component is not found, even in same dir

I'm creating C++ / QML application. In debug version I want to load QML directly from file and in release build I want to load QML from resources. I want to use QDir::setSearchPaths for this:

    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
    }

I'm loading components in next way:

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

When I use a full path in loadUrl - everything is OK, but when I use qml:/MainWindow.qml - file is found, but it can't load components, that are placed in same folder (simplified):

MainWindow.qml

Window {
  id: root
  visible: true
  CustomComponent {
  }
}

CustomComponent.qml

Rectangle {
  id: root
  color: "red"
}

How to resolve it? (How to set lookup folders in qml engine via searchPath)

Upvotes: 2

Views: 1086

Answers (1)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

Solved by making url in next way:

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

Upvotes: 1

Related Questions