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. How to change the font and color of QString?
QtWS25 Last Chance

How to change the font and color of QString?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qstringqt 5.4
12 Posts 3 Posters 5.1k Views
  • 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.
  • L Offline
    L Offline
    learnist
    wrote on 19 Aug 2020, 17:36 last edited by
    #1

    i have a QString like this ">>> This is an Arrow." , now i want to have >>> in bold and red, How to do that ?

    i have looked over some pages and found this syntax

    QString redPart = QString("<span style=" color:#ff0000;">%1</span>").arg(">>>");

    but it gives some error like ")" missing and, i am not able to figure it out.

    J 1 Reply Last reply 19 Aug 2020, 17:48
    0
    • L learnist
      19 Aug 2020, 17:36

      i have a QString like this ">>> This is an Arrow." , now i want to have >>> in bold and red, How to do that ?

      i have looked over some pages and found this syntax

      QString redPart = QString("<span style=" color:#ff0000;">%1</span>").arg(">>>");

      but it gives some error like ")" missing and, i am not able to figure it out.

      J Offline
      J Offline
      JonB
      wrote on 19 Aug 2020, 17:48 last edited by JonB
      #2

      @learnist
      Look at your " (double-quote) characters in the literal string after style= in your string and sort them out (C++ literal strings).

      A QString in itself doesn't have colors. Which Qt widget are you intending to display this colored string in?

      L 1 Reply Last reply 19 Aug 2020, 17:55
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 19 Aug 2020, 17:49 last edited by
        #3

        Hi,

        What is the exact error you get ?
        Where exactly do you want that color to appear ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply 19 Aug 2020, 17:59
        0
        • J JonB
          19 Aug 2020, 17:48

          @learnist
          Look at your " (double-quote) characters in the literal string after style= in your string and sort them out (C++ literal strings).

          A QString in itself doesn't have colors. Which Qt widget are you intending to display this colored string in?

          L Offline
          L Offline
          learnist
          wrote on 19 Aug 2020, 17:55 last edited by
          #4

          @JonB It is going to be the text of a List item in a QList Widget.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 19 Aug 2020, 17:58 last edited by
            #5

            In that case, you should check the various item data roles of Qt's item view system especially ForegroundRole.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S SGaist
              19 Aug 2020, 17:49

              Hi,

              What is the exact error you get ?
              Where exactly do you want that color to appear ?

              L Offline
              L Offline
              learnist
              wrote on 19 Aug 2020, 17:59 last edited by
              #6

              @SGaist Hi, i want the color to appear red of ">>>" and i get error on color and #
              d9964ea2-fa34-417b-80c4-35500811016a-image.png
              696f9a11-6640-458d-9e7e-8fa332e02c07-image.png

              J 1 Reply Last reply 19 Aug 2020, 18:01
              0
              • L learnist
                19 Aug 2020, 17:59

                @SGaist Hi, i want the color to appear red of ">>>" and i get error on color and #
                d9964ea2-fa34-417b-80c4-35500811016a-image.png
                696f9a11-6640-458d-9e7e-8fa332e02c07-image.png

                J Offline
                J Offline
                JonB
                wrote on 19 Aug 2020, 18:01 last edited by JonB
                #7

                @learnist
                I have already told you why you get an error in your code. Why bother if you're going to ignore it? You should read up how to put " character into C++ string literals, you need to know how to do that to code successfully.

                Once you have done that proceed the way you were intending. That will allow you to mix colors in a QListWidgetItem.

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 19 Aug 2020, 18:02 last edited by
                  #8

                  For the special rendering you want, you will have to implement a QStyledItemDelegate.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply 19 Aug 2020, 18:04
                  0
                  • S SGaist
                    19 Aug 2020, 18:02

                    For the special rendering you want, you will have to implement a QStyledItemDelegate.

                    J Offline
                    J Offline
                    JonB
                    wrote on 19 Aug 2020, 18:04 last edited by JonB
                    #9

                    @SGaist
                    I do not see why he needs to do as you say?! :) Since QListWidgetItem does accept HTML, what's wrong with just using that? I don't see the need for ForegroundRole or for a delegate? Remmeber he wants certain characters with different color/weight, easy to do in HTML.

                    S 1 Reply Last reply 19 Aug 2020, 18:11
                    0
                    • J JonB
                      19 Aug 2020, 18:04

                      @SGaist
                      I do not see why he needs to do as you say?! :) Since QListWidgetItem does accept HTML, what's wrong with just using that? I don't see the need for ForegroundRole or for a delegate? Remmeber he wants certain characters with different color/weight, easy to do in HTML.

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 19 Aug 2020, 18:11 last edited by SGaist
                      #10

                      @JonB said in How to change the font and color of QString?:

                      @SGaist
                      I do not see why he needs to do as you say?! :) Since QListWidgetItem does accept HTML, what's wrong with just using that? I don't see the need for ForegroundRole or for a delegate?

                      ForegroundRole to change the color of the text but I forgot it was only for a small portion of it.

                      I don't didn't remember QListWidgetItem supporting html out of the box, that's why I suggested the delegate.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J 1 Reply Last reply 19 Aug 2020, 18:15
                      1
                      • S SGaist
                        19 Aug 2020, 18:11

                        @JonB said in How to change the font and color of QString?:

                        @SGaist
                        I do not see why he needs to do as you say?! :) Since QListWidgetItem does accept HTML, what's wrong with just using that? I don't see the need for ForegroundRole or for a delegate?

                        ForegroundRole to change the color of the text but I forgot it was only for a small portion of it.

                        I don't didn't remember QListWidgetItem supporting html out of the box, that's why I suggested the delegate.

                        J Offline
                        J Offline
                        JonB
                        wrote on 19 Aug 2020, 18:15 last edited by JonB
                        #11

                        @SGaist said in How to change the font and color of QString?:

                        I don't remember QListWidgetItem supporting html out of the box, that's why I suggested the delegate.

                        I trusted https://stackoverflow.com/questions/22247904/how-to-style-rich-text-in-qlistwidgetitem-and-qcombobox-items-pyqt-pyside

                        You could use html/css-likes styles, i.e just wrap your text inside tags:
                        item.setData( QtCore.Qt.UserRole, "<b>{0}</b>".format('data to store for this QListWidgetItem'))

                        and others. Assuming it does, it will be a lot simpler for the OP than writing a delgate.

                        Whoops, OK, I see that was UserRole... One person said it did work, one said it did not? OK, maybe QListWidgetItem does not support rich text :(

                        S 1 Reply Last reply 19 Aug 2020, 18:23
                        0
                        • J JonB
                          19 Aug 2020, 18:15

                          @SGaist said in How to change the font and color of QString?:

                          I don't remember QListWidgetItem supporting html out of the box, that's why I suggested the delegate.

                          I trusted https://stackoverflow.com/questions/22247904/how-to-style-rich-text-in-qlistwidgetitem-and-qcombobox-items-pyqt-pyside

                          You could use html/css-likes styles, i.e just wrap your text inside tags:
                          item.setData( QtCore.Qt.UserRole, "<b>{0}</b>".format('data to store for this QListWidgetItem'))

                          and others. Assuming it does, it will be a lot simpler for the OP than writing a delgate.

                          Whoops, OK, I see that was UserRole... One person said it did work, one said it did not? OK, maybe QListWidgetItem does not support rich text :(

                          S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 19 Aug 2020, 18:23 last edited by
                          #12

                          @JonB tests are in order ! It would indeed be simpler.

                          As I miswrote (and fixed) it's possibly a memory issu :-).

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          10/12

                          19 Aug 2020, 18:11

                          • Login

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