Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt/QML: Text with inline QML elements

We are building a graphical user interface with QtQuick/QML. We have some dynamic, multi-line text coming from a database, which should be displayed in the application. Currently, we are using the Text element to display the text. However, we need some QML components inline embedded into the text. For this, the text coming from the database contains placeholders such as ::checkbox|1:: which should then be replaced and displayed by the program.

In HTML, this is easy, you can simply mix inline elements with text to produce a result like this:

enter image description here

but in QML, this seems to be more difficult, as Text elements cannot be word-wrapped into two halves if there is not enough space (both the text and the container size should be dynamic).

The best solution we could come up with, is creating a Flow layout with one Text element for each word, but this seems too hacky. Using RichText with HTML is not enogh, since we really need our custom QML elements in the text. Also, we want to avoid using a WebView due to performance reasons.

Is there a sophisticated way to implement this with QML/C++ only?

like image 630
Œlrim Avatar asked Feb 14 '18 21:02

Œlrim


People also ask

How do I display text in QML?

To display text in QML, create a Text item and set the text property to the text you wish to display. The Text item will now display that text. Several properties can be set on the Text item to style the entire block of text. These include color, font family, font size, bold and italic.

How do I add padding to QML?

To specify a padding value for a specific edge of the control, set its relevant property: See also Control Layout, availableWidth, availableHeight, topPadding, leftPadding, rightPadding, and bottomPadding. This property holds the right inset for the background. This property was introduced in QtQuick.

What is elide in QML?

elide : enumeration. font.bold : bool.

What is text in QML?

The Text item allows you to add formatted text to a scene. More... Inherits Item. This element was introduced in Qt 4.7.


1 Answers

You can create custom widgets and embed them into QML: Writing QML Extensions with C++

like image 142
ephemerr Avatar answered Sep 17 '22 14:09

ephemerr