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 set text from external file to QML Text element?
Forum Updated to NodeBB v4.3 + New Features

How to set text from external file to QML Text element?

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 3 Posters 8.3k 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.
  • M Offline
    M Offline
    moo1
    wrote on last edited by
    #1

    I have a local HTML file that I want to show on my app. I can use, and indeed am using, WebView but I feel it's a bit overkill as the HTML file is static and contains only minimum styles, and I'm pretty sure Text element can handle it. However, Text element doesn't seem to have a way to specify text content from external file. Moreover, QML does not have any API to read a local file.

    Does anyone have any workaround idea? Copy-paste HTML content into QML code works but it's ugly and hard to maintain.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I just build a very simple loader component in C++, and used that to set the text via QML property binding. That works well.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moo1
        wrote on last edited by
        #3

        I see. I wonder if there's QML only solution but seems I have to use C++. Thanks!

        btw, I wonder if there's a place to share such a QML "goodies" - useful small code snippets for QML?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I am sorry I can not share this particular piece of code, as it was written for a commercial project. However, based on QNetworkAccessManager, it really was quite easy to do. I just used two properties: a URL property and a content property. Set the url, and the content will be fetched by the QNAM.

          1 Reply Last reply
          0
          • AlicemirrorA Offline
            AlicemirrorA Offline
            Alicemirror
            wrote on last edited by
            #5

            Hi, I find interesting your method, Andre.

            In prevision to develop a project in a multi-language environment I am approaching the use of QT-Linguist as text files management. I have not investigated in-depth, but from the documentation I read it seems that can also be used in a QML-only environment, what do you think about this kind of solution ?

            Enrico Miglino (aka Alicemirror)
            Balearic Dynamics
            Islas Baleares, Ibiza (Spain)
            www.balearicdynamics.com

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              I am not sure I understand either the problem you are trying to solve, or the method you use to solve it. Please elaborate. Note that QML at the moment does not support dynamic translation, that is, switching the language during the running of your application like you can in the widget world. I don't know if that impacts your plans?

              1 Reply Last reply
              0
              • AlicemirrorA Offline
                AlicemirrorA Offline
                Alicemirror
                wrote on last edited by
                #7

                I read in the documentation that QML does not support multi-language on runtime (I also tested to be sure :) )

                The possibilities I found at the moment are two:

                Set the language support in a C++ class that works in conjunction with QT-Linguist features and at runtime "serves" the needs of the QML calls.

                Create a multi-language version of the where the user can choice the main language at the installation time.

                Then, is possible that both these options can stay together in the app. Do you think it is possible?

                Enrico Miglino (aka Alicemirror)
                Balearic Dynamics
                Islas Baleares, Ibiza (Spain)
                www.balearicdynamics.com

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  I am sure there is some scheme possible to make this work, but I doubt it will be very elegant.

                  1 Reply Last reply
                  0
                  • AlicemirrorA Offline
                    AlicemirrorA Offline
                    Alicemirror
                    wrote on last edited by
                    #9

                    I have not yet found any example of QML + Multi language, but I think that exist applications. Why not elegant? I am thinking to something like C+ class that can be called to show text elements in QML and the C++ construct work with the language parameters.

                    The command line tool of QT-Linguist that extract the text from the source to create language files, does it work only with c sources ?

                    Enrico Miglino (aka Alicemirror)
                    Balearic Dynamics
                    Islas Baleares, Ibiza (Spain)
                    www.balearicdynamics.com

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      QML does work with i18n. You can put strings to be translated in the QML source. The only thing that does not work, is dynamic translation: changing the language of the application while it is running. If you just need a translatable QML app, everything you need is already there.

                      Not elegant is something like this, IMHO:
                      @
                      //pseudocode
                      Text {
                      text: translator.text1;
                      }
                      @

                      Where translator is some C++ object exported to Qt to provide the translations. It is just hard to maintain and read. Currently, you do this:

                      @
                      Text {
                      text: qsTr("The text to translate");
                      }
                      @

                      That works, but only works once. The text is not re-set when the language changes. The elegant way to solve this, IMHO, would be if qsTr would be changed from something like a script call (only executed once) to a binding to an object that updates the text when the language changes. That, however, requires changes in Qt Quick. I think there already is a bugreport on this. You may considder voting for it.

                      1 Reply Last reply
                      0
                      • AlicemirrorA Offline
                        AlicemirrorA Offline
                        Alicemirror
                        wrote on last edited by
                        #11

                        Hi,

                        I agree that your pseudocode is not elegant. And probably is also less flexible than the C++
                        A workaround to this aspect - I think, but not yet tested - should be an external set of java functions dedicated to the translation. Or a C++ dedicated class.

                        Regarding the fact that can't change language at runtime, I have a doubt: if C++ can, demanding text management to C++ code interfaced to QML should it possible that can work? As a matter of fact, QML logic only trigger events that influence language changes while the text delivery in a specific language is fully managed by C++ calls.

                        Enrico Miglino (aka Alicemirror)
                        Balearic Dynamics
                        Islas Baleares, Ibiza (Spain)
                        www.balearicdynamics.com

                        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