Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Is it possible to colorize only a part of a string in QML?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Is it possible to colorize only a part of a string in QML?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #3

    AFAIK, the closest thing to do it in pure QML only is to use the HTML tags.
    For e.g
    @
    Text {
    anchors.centerIn: parent
    font.pointSize: 18.0
    text: "<b>T</b>h<b>i</b>s i<i>s</i> a <font color='red'>T</font>es<font color='green'>T</font>"
    }
    @

    157

    1 Reply Last reply
    0
    • S Offline
      S Offline
      skammers
      wrote on last edited by
      #4

      Thanks! Thats perfect! :)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        skammers
        wrote on last edited by
        #5

        What if I wanted to do it from C++?=)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #6

          Hi, it's (nearly) the same:

          @
          ui->label->setText("<b>T</b>h<b>i</b>s i<i>s</i> a <font color='red'>T</font>es<font color='green'>T</font>");
          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            skammers
            wrote on last edited by
            #7

            I have a QString which I want to be in the text field, in different styles.

            So I have:

            @
            QString text = "<b>Test</b>";
            @

            but in the TextField in QML, it displays <b>Test</b> instead of
            "Test" in bold style. How can I change that?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              clochydd
              wrote on last edited by
              #8

              How do you transform the QString text to QML?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                skammers
                wrote on last edited by
                #9

                @
                Q_PROPERTY(QString test READ test NOTIFY testChanged)
                @

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #10

                  Create you own QML component by subclassing the "QQuickPaintedItem":http://qt-project.org/doc/qt-5/qquickpainteditem.html and override paint() method and draw the text with the styles as per your need. Check "QPainter":http://qt-project.org/doc/qt-4.8/qpainter.html api's. You will need "setPen":http://qt-project.org/doc/qt-4.8/qpainter.html#setPen to change the font color or its style according to your need.

                  157

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #11

                    Also,

                    bq. but in the TextField in QML, it displays <b>Test</b> instead of
                    “Test” in bold style. How can I change that?

                    TextField doesnot have ability to display Rich Texts. Use TextArea instead,
                    @
                    TextArea {
                    width: 240
                    text: "<b>T</b>h<b>i</b>s i<i>s</i> a <font color='red'>T</font>es<font color='green'>T</font>"
                    textFormat: TextEdit.RichText
                    }
                    @

                    157

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      skammers
                      wrote on last edited by
                      #12

                      Thank you! Now I really got it to work.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved