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. Can't load text file from .qrc
Forum Updated to NodeBB v4.3 + New Features

Can't load text file from .qrc

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 822 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by kitfox
    #1

    I've created an xml file that I'd like to be able to load from a .qrc file, but it's not working for some reason.

    My qrc file is in resources/mainresources.qrc and I seem to be able to load icon images from it fine. For example, QIcon(":/icons/toolBrushVector.svg") can be used to draw an icon.

    I've created a file called mainmenu.xml and placed it under resources/. I then added an entry to the qrc index:

    </qresource>
        ....
        <file>mainmenu.xml</file>
    </qresource>
    

    I then attempt to load the file into a string like this:

    QFile file(":/mainmenu.xml");
    QTextStream in(&file);
    QString xmlStrn = in.readAll();
    file.close();
    

    Unfortunately, xmlStrn keeps being set to "". What can I do to load my xml file?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kitfox
      wrote on last edited by
      #2

      Looks like the problem wasn't with the qrc. I needed to load the text like this:

      QFile file(":/mainmenu.xml");
      file.open(QIODevice::ReadOnly);
      QString xml = file.readAll();
      file.close();
      
      jsulmJ 1 Reply Last reply
      0
      • K kitfox

        Looks like the problem wasn't with the qrc. I needed to load the text like this:

        QFile file(":/mainmenu.xml");
        file.open(QIODevice::ReadOnly);
        QString xml = file.readAll();
        file.close();
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @kitfox You can use QTextStream, but you have to open the file first:

        QFile file(":/mainmenu.xml");
        if (data.open(QFile::ReadOnly)) {
            QTextStream in(&file);
            QString xmlStrn = in.readAll();
            file.close();
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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