Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++ qrc file access

C++ qrc file access

Scheduled Pinned Locked Moved Unsolved General and Desktop
qrc
2 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved