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 ":/"

    thanks!

    img

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

    var readResult = fileio.read(":/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 ( file.open(QIODevice::ReadOnly) ) {
            QString line;
            QTextStream t( &file );
            t.setCodec("UTF-8");
            do {
                line = t.readLine();
                fileContent += line;
            } while (!line.isNull());
    
            file.close();
        } else {
            emit error("Unable to open the file");
            return QString();
        }
        return fileContent;
    }
    

  • Qt Champions 2016

    @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
    view->setUrl(QUrl("qrc:/dial.qml"));

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.