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 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.

    JonBJ 1 Reply Last reply
    0
    • L learnist

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        0
        • JonBJ JonB

          @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 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            • SGaistS SGaist

              Hi,

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

              L Offline
              L Offline
              learnist
              wrote on 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

              JonBJ 1 Reply Last reply
              0
              • L learnist

                @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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 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

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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.

                    SGaistS 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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.

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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

                      JonBJ 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        @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.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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 :(

                        SGaistS 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @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 :(

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 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

                          • Login

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