Nominate our 2022 Qt Champions!

C++ qrc file access

  • Just want to clarify a detail here: with the FileIO::read(const QString& Url) method below, if I use a qrc path, I get an error. If I use a relative path (";/") it works. I didn't set a prefix for the qrc resources – I just use the default.

    Can someone explain (for my Qt fatigued brain) what the difference here is and why the qrc path doesn't resolve to ":/"



    var readResult ="qrc:/test.json"); // this doesn't work

    var readResult =":/test.json"); // this does

    QString FileIO::read(const QString& Url)
        m_source = Url;
        emit error(m_source);
        if (m_source.isEmpty()){
            emit error("source is empty");
            return QString();
        QFile file(m_source);
        QString fileContent;
        if ( ) {
            QString line;
            QTextStream t( &file );
            do {
                line = t.readLine();
                fileContent += line;
            } while (!line.isNull());
        } else {
            emit error("Unable to open the file");
            return QString();
        return fileContent;

  • Lifetime Qt Champion

    @TOMATO_QT said:
    I did wonder the same thing
    and it seems to me that C++ use the ":/"
    and QML
    likes the "qrc:/"
    I have seen code do

    So to make it ":/" it might need a to go though QUrl ?
    Not tested, just pure speculation.

Log in to reply