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

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

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

  • 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?

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

  • 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 ?

  • 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?

  • 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?

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

  • 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 ?

  • 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:
    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.

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

Log in to reply

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