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:
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?
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.
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.
elide : enumeration. font.bold : bool.
The Text item allows you to add formatted text to a scene. More... Inherits Item. This element was introduced in Qt 4.7.
You can create custom widgets and embed them into QML: Writing QML Extensions with C++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With