Stefan Monov Stefan Monov - 1 year ago 125
C++ Question

Difference between `:/foo`, `qrc:/foo` and `qrc:///foo` paths in Qt

This code succeeds:

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));


This code prints "failure":

QFile file("qrc:/main.qml");
if ( file.open(QIODevice::ReadOnly) ) {
cout << "success" << endl;
} else {
cout << "failure" << endl;
}


Changing the QFile constructor argument to
qrc:///main.qml
(as in the Qt documentation) doesn't fix it. Changing it to
:/main.qml
makes it print "success" though.

So I'm confused as to when to use each of the 3 forms.

Answer Source

As stated in your link, :/main.qml is a file path, while qrc:/main.qml is an url.

The former can be used where a file path is required -- basically a QString, like with QFile constructor.

The latter can be used where an url is required, like with QQmlApplicationEngine::load through QUrl. It can not be used with QFile constructor where a path is needed.

About qrc:///, a simplified url syntax (without query, user/password, nor port) can be:

scheme:[//host][/]path

Which can lead to scheme:/path if you skip host part, or scheme:///path if you specify host as an empty string.

qrc:/main.qml and qrc:///main.qml are just two writings of the same url.

I do not know if an empty host is valid according to the corresponding RFCs, or if this is specific to Qt url implementation.

See here for complete url syntax.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download