Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to load contents of file from resources for TextArea 'text' property?

How to load contents of file from resources for TextArea 'text' property?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 1 Posters 1.2k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    reedhedges
    wrote on last edited by
    #1

    Hello,

    I'd like to read the contents of a file (from application resources) as the text property of a TextArea. What is the best way to do this? I haven't found anything in the docs or by searching that help.

    I can imagine a couple possible ways...

    1. text: readFile('qrc:/myapp/file.html')
    2. Component.onCompleted: { text = readFile('qrc:/myapp/file.html'); }

    But how to implement readFile()? Is there a javascript function to read files from resources? XMLHtttpRequest??

    1. This text is not dynamic, so some way to tell the QML parser to inline the file contents in the QML string would work too.

    Is there documentation on what parts of the Javascript/Browser/Etc API are available in QML? I have discovered by trial and error what parts of the DOM/Document/Element/Node APIs are implemented, but it would be nice if there was API documentation. Or maybe a few files in the Qt source code that can be used for reference?

    (This TextArea is inside a ScrollView in a Dialog; it's purpose is just to show some documentation/information to the user. Is there another QML component I'm not aware of that is better suited to this?)

    Thanks!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      reedhedges
      wrote on last edited by
      #2

      I'd also like to eventually have translations for my text file as well, if that is a consideration on the best way to do this.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        reedhedges
        wrote on last edited by reedhedges
        #3

        OK I did come up with a solution for now, but don't know if it's the best one:

        I just added a QMLUtil C++ class (derived from QObject) with a method Q_INVOKABLE QString readFile(const QString& path);. This just uses QFile::readAll() to read the file and return the contents as a QString.

        I then passed an instance of this to QML engine in main() with:

        QMLUtil qmlUtil;
        QQmlApplicationEngine.rootContext()->setContextProperty("QMLUtil", &qmlUtil);

        and can use it in my TextArea:

        text: visible ? QMLUtil.readFile(":myapp/file.html") : ""

        (Note need to use QFile's file path syntax with : prefix instead of qrc:/.)

        Pretty simple (doesn't do much with errors, just throws an exception or returns empty string), and would probably have some issues if the file were very large, but seems to work for now. If there was a pure Javascript solution, that would be better for some of my other QML/JS-only projects however.

        Let me know if you have other ideas on how to do this!

        Thanks

        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