Search code examples
qtqtquick2qt-quick

Qt 5.7 Tagging alternating words in text element to be bold


So I want something equivalent to

<p>The <b>quick</b> brown <b>fox</b> jumps <b>over</b> the <b>lazy</b> dog</p>

The quick brown fox jumps over the lazy dog

I know I can achieve this with a Flow in combination with a bunch of Text elements, one for each contiguous block of bold or non-bold text. I was just wondering if there was something cleaner.


Solution

  • See QML Text item documentation. There's even an example:

    Text {
        text: "<b>Hello</b> <i>World!</i>"
    }
    

    Copy the following in the awesome QML web editor

    import QtQuick 2.0
    
    Rectangle {
      width: 400; height: 100
    
      Text {
         text: "The <b>quick</b> brown <b>fox</b> jumps <b>over</b> the <b>lazy</b> dog"
         anchors.verticalCenter: parent.verticalCenter
         anchors.horizontalCenter: parent.horizontalCenter
      }
    }