March 3, 2011

moo1 moo1
Lab Rat
33 posts

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.

10 replies

March 4, 2011

Andre Andre
Robot Herder
6296 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 4, 2011

moo1 moo1
Lab Rat
33 posts

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?

March 5, 2011

Andre Andre
Robot Herder
6296 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 5, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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 ?

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

March 6, 2011

Andre Andre
Robot Herder
6296 posts

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?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 6, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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:

  1. 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.
  2. 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?

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

March 6, 2011

Andre Andre
Robot Herder
6296 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 6, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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 ?

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

March 6, 2011

Andre Andre
Robot Herder
6296 posts

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:

  1. //pseudocode
  2. Text {
  3.   text: translator.text1;
  4. }

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:

  1. Text {
  2.    text: qsTr("The text to translate");
  3. }

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 6, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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.

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

 
  ‹‹ [solved]A listView in a listview      QDeclarativeView problem ››

You must log in to post a reply. Not a member yet? Register here!