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. Custom CSS style in Textedit
Forum Updated to NodeBB v4.3 + New Features

Custom CSS style in Textedit

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 2.3k Views 2 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.
  • B Brad1111

    @JonB I wasnt sure so i tested both , with same result

    SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #7

    @Brad1111 Hi,

    Can you show an complete example of html you try to apply ?

    The engine behind QTextEdit only supports a subset of HTML4 markup as defined here. If you want to style elements, you would usually go with inline styling <node style=""></node>

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

    B 1 Reply Last reply
    1
    • JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @SGaist
      Yes, but the OP is asking how to achieve this via a class style rule in CSS, so that e.g. he can easily change just that style rule to affect all HTML elements with that class.

      @Brad1111
      I fired up my Qt to test for you :) I don't know exactly what you are doing, especially with the specification of the style rule, but

          QTextEdit w;
          w.setHtml("<style>.simple-highlight { color:green; }</style> \
                     <span style='color: red'>Some red text</span><br> \
                     <span class='simple-highlight'>Some green text</span><br>");
      

      works fine. Tested under Qt 5.15. I don't have Qt6, but it will work, or that's a bug.

      1 Reply Last reply
      0
      • SGaistS SGaist

        @Brad1111 Hi,

        Can you show an complete example of html you try to apply ?

        The engine behind QTextEdit only supports a subset of HTML4 markup as defined here. If you want to style elements, you would usually go with inline styling <node style=""></node>

        B Offline
        B Offline
        Brad1111
        wrote on last edited by Brad1111
        #9

        @SGaist said in Custom CSS style in Textedit:

        If you want to style elements, you would usually go with inline styling <node style=""></node>

        Hi, could you give an example with node style ? I couldnt find anything about that ...

        @JonB Thanks, your example works indeed, for some reason. But whenever I try to split this in 2 parts i.e. one loading part and one text part, it doesnt work anymore. It seems like Im pretty bad but I cant figure that out so far ... I tried splitting the set HTML into 2 sethtml and also with my original load css file through :

        with open("css_style.css", 'r') as file: 
        			sheet = file.read() 
        		#self.frame4_text.setStyleSheet(sheet)
                        self.frame4_text.appendHtml(sheet)
        
        self.frame4_text.appendHtml("<style>.simple-highlight </style><span class='simple-highlight'>Some green text</span> ")   # this works
        self.frame4_text.appendHtml("<span class='simple-highlight'>Some green text</span> ")  # this does not work
        
        JonBJ 1 Reply Last reply
        0
        • B Brad1111

          @SGaist said in Custom CSS style in Textedit:

          If you want to style elements, you would usually go with inline styling <node style=""></node>

          Hi, could you give an example with node style ? I couldnt find anything about that ...

          @JonB Thanks, your example works indeed, for some reason. But whenever I try to split this in 2 parts i.e. one loading part and one text part, it doesnt work anymore. It seems like Im pretty bad but I cant figure that out so far ... I tried splitting the set HTML into 2 sethtml and also with my original load css file through :

          with open("css_style.css", 'r') as file: 
          			sheet = file.read() 
          		#self.frame4_text.setStyleSheet(sheet)
                          self.frame4_text.appendHtml(sheet)
          
          self.frame4_text.appendHtml("<style>.simple-highlight </style><span class='simple-highlight'>Some green text</span> ")   # this works
          self.frame4_text.appendHtml("<span class='simple-highlight'>Some green text</span> ")  # this does not work
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #10

          @Brad1111
          One more time, as I keep saying: I presume your css_style.css file does not enclose its content in <style> ... </style> tags? Yet contains style? Try inserting those yourself, maybe

          self.frame4_text.appendHtml("<style>" + sheet + "</style>")
          

          Not sure about appendHtml() vs setStyleSheet(). (And the only appendHtml() I could spot was one for QPlainTextEdit but I assume you know what you are doing here.)

          Get it working with literal text as required, then make sure that is reflected (or adjusted) for whatever you read from file.

          P.S.

          self.frame4_text.appendHtml("<style>.simple-highlight </style><span class='simple-highlight'>Some green text</span> ")   # this works
          

          Don't know what "works" means. As it stands, <style>.simple-highlight </style> defines an empty style.

          Hi, could you give an example with node style ? I couldnt find anything about that ...

          @SGaist simply mean <somehtmltag style="..."> ... </somehtmltag>, i.e. inline-specified style each time, not your shared-re-used-class which you are asking for.

          B 1 Reply Last reply
          0
          • JonBJ JonB

            @Brad1111
            One more time, as I keep saying: I presume your css_style.css file does not enclose its content in <style> ... </style> tags? Yet contains style? Try inserting those yourself, maybe

            self.frame4_text.appendHtml("<style>" + sheet + "</style>")
            

            Not sure about appendHtml() vs setStyleSheet(). (And the only appendHtml() I could spot was one for QPlainTextEdit but I assume you know what you are doing here.)

            Get it working with literal text as required, then make sure that is reflected (or adjusted) for whatever you read from file.

            P.S.

            self.frame4_text.appendHtml("<style>.simple-highlight </style><span class='simple-highlight'>Some green text</span> ")   # this works
            

            Don't know what "works" means. As it stands, <style>.simple-highlight </style> defines an empty style.

            Hi, could you give an example with node style ? I couldnt find anything about that ...

            @SGaist simply mean <somehtmltag style="..."> ... </somehtmltag>, i.e. inline-specified style each time, not your shared-re-used-class which you are asking for.

            B Offline
            B Offline
            Brad1111
            wrote on last edited by Brad1111
            #11

            @JonB Nah, I put this in my css, as you told me :

            <style>.simple-highlight { color:red; }</style>
            

            and it does not work. even without the loading css, if i just split the sethtml into

            #styling loading
            self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style>")
            
            self.frame4_text.appendHtml("<span style='color: red'>Some red text</span>")
            self.frame4_text.appendHtml("<span class='simple-highlight'>Some green text</span> ")
            
            this does not work either
            

            Edited :
            And also, I have to be clear on the fact that this code works as intended :

            self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style><span class='simple-highlight'>Some green text</span> ")
            

            but it looks more like an inline method if i have to specify it everytime

            added link : https://doc.qt.io/qt-6/qplaintextedit.html#appendHtml

            JonBJ 3 Replies Last reply
            0
            • B Brad1111

              @JonB Nah, I put this in my css, as you told me :

              <style>.simple-highlight { color:red; }</style>
              

              and it does not work. even without the loading css, if i just split the sethtml into

              #styling loading
              self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style>")
              
              self.frame4_text.appendHtml("<span style='color: red'>Some red text</span>")
              self.frame4_text.appendHtml("<span class='simple-highlight'>Some green text</span> ")
              
              this does not work either
              

              Edited :
              And also, I have to be clear on the fact that this code works as intended :

              self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style><span class='simple-highlight'>Some green text</span> ")
              

              but it looks more like an inline method if i have to specify it everytime

              added link : https://doc.qt.io/qt-6/qplaintextedit.html#appendHtml

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #12

              @Brad1111
              OK. Could you please provide me documentation link for whatever class you are using appendHtml(...) on?

              1 Reply Last reply
              0
              • B Brad1111

                @JonB Nah, I put this in my css, as you told me :

                <style>.simple-highlight { color:red; }</style>
                

                and it does not work. even without the loading css, if i just split the sethtml into

                #styling loading
                self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style>")
                
                self.frame4_text.appendHtml("<span style='color: red'>Some red text</span>")
                self.frame4_text.appendHtml("<span class='simple-highlight'>Some green text</span> ")
                
                this does not work either
                

                Edited :
                And also, I have to be clear on the fact that this code works as intended :

                self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style><span class='simple-highlight'>Some green text</span> ")
                

                but it looks more like an inline method if i have to specify it everytime

                added link : https://doc.qt.io/qt-6/qplaintextedit.html#appendHtml

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #13

                @Brad1111 said in Custom CSS style in Textedit:

                Edited : added link : https://doc.qt.io/qt-6/qplaintextedit.html#appendHtml

                Which like I said earlier is the only one I could find. Why would you choose a plain text edit if you want to put in HTML markup? Seems bizarre to me that it even accepts HTML. Have you tried a QTextEdit? Would you like to do so before I spend time changing my code over to QPlainTextEdit? Maybe it's not an issue, but we have to be consistent....

                Your post started with

                I have a Qtextedit in which I display several lines of text over time.

                You must be exact! You are now showing using a QPlainTextEdit when you said QTextEdit...

                I'm playing with differences between setting/appending and QTextEdit vs QPlainTextEdit. Solution/workaround may depend on which class. Can you let me know whether you must use QPlainTextEdit or QTextEdit or don't care?

                1 Reply Last reply
                2
                • B Brad1111

                  @JonB Nah, I put this in my css, as you told me :

                  <style>.simple-highlight { color:red; }</style>
                  

                  and it does not work. even without the loading css, if i just split the sethtml into

                  #styling loading
                  self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style>")
                  
                  self.frame4_text.appendHtml("<span style='color: red'>Some red text</span>")
                  self.frame4_text.appendHtml("<span class='simple-highlight'>Some green text</span> ")
                  
                  this does not work either
                  

                  Edited :
                  And also, I have to be clear on the fact that this code works as intended :

                  self.frame4_text.appendHtml("<style>.simple-highlight { color:blue; }</style><span class='simple-highlight'>Some green text</span> ")
                  

                  but it looks more like an inline method if i have to specify it everytime

                  added link : https://doc.qt.io/qt-6/qplaintextedit.html#appendHtml

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #14

                  @Brad1111
                  OK, I have done some playing. Here are your two choices.

                  If you must use QPlainTextEdit:
                  You cannot use appendHtml() multiple times, you can use it once and once only on empty. You must save for yourself all the stuff which you previously called to append (QPlainTextEdit does not provide this to you), then clear and append anew. Like

                  self.currentHtml = ""
                  
                  // every time you want to append some more stuff
                  self.currentHtml += nextStuff
                  self.frame4_plaintextedit.clear()
                  self.frame4_plaintextedit.appendHtml(self.currentHtml)
                  

                  You are "lucky" QPlainTextEdit even accepts/acts on any HTML. Personally I wouldn't use it for this task, unless you have to (and it may be more efficient for large texts).

                  If you use QTextEdit:
                  There isn't any appendHtml() anyway. You must follow the same principle as above, including having to save the HTML used so far useful (QTextEdit.toHtml() cannot be used to correctly retrieve what you have already put there, trust me.)

                  self.currentHtml = ""
                  
                  // every time you want to append some more stuff
                  self.currentHtml += nextStuff
                  self.frame4_textedit.setHtml(self.currentHtml)
                  

                  [There are neater, more self-contained ways of achieving this than keeping the text in a form class member variable, such as storing it in a property of the edit widget or subclassing, but to keep this simple I leave that as an exercise for the reader :) ]

                  I have not tried it, but you could perhaps use QTextEdit.insertHtml() with appropriate positioning of the text cursor. Otherwise you could go to the QTextDocument (returned by document()) underlying both of these widgets and try to do your work there (see also setDefaultStyleSheet() for applying the stylesheet to the whole of the text document instead of the default current-block-only). These might be more efficient on large documents, but for your purposes probably just more complicated than code shown.

                  You owe me a beer ;-)

                  B 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Brad1111
                    OK, I have done some playing. Here are your two choices.

                    If you must use QPlainTextEdit:
                    You cannot use appendHtml() multiple times, you can use it once and once only on empty. You must save for yourself all the stuff which you previously called to append (QPlainTextEdit does not provide this to you), then clear and append anew. Like

                    self.currentHtml = ""
                    
                    // every time you want to append some more stuff
                    self.currentHtml += nextStuff
                    self.frame4_plaintextedit.clear()
                    self.frame4_plaintextedit.appendHtml(self.currentHtml)
                    

                    You are "lucky" QPlainTextEdit even accepts/acts on any HTML. Personally I wouldn't use it for this task, unless you have to (and it may be more efficient for large texts).

                    If you use QTextEdit:
                    There isn't any appendHtml() anyway. You must follow the same principle as above, including having to save the HTML used so far useful (QTextEdit.toHtml() cannot be used to correctly retrieve what you have already put there, trust me.)

                    self.currentHtml = ""
                    
                    // every time you want to append some more stuff
                    self.currentHtml += nextStuff
                    self.frame4_textedit.setHtml(self.currentHtml)
                    

                    [There are neater, more self-contained ways of achieving this than keeping the text in a form class member variable, such as storing it in a property of the edit widget or subclassing, but to keep this simple I leave that as an exercise for the reader :) ]

                    I have not tried it, but you could perhaps use QTextEdit.insertHtml() with appropriate positioning of the text cursor. Otherwise you could go to the QTextDocument (returned by document()) underlying both of these widgets and try to do your work there (see also setDefaultStyleSheet() for applying the stylesheet to the whole of the text document instead of the default current-block-only). These might be more efficient on large documents, but for your purposes probably just more complicated than code shown.

                    You owe me a beer ;-)

                    B Offline
                    B Offline
                    Brad1111
                    wrote on last edited by
                    #15

                    @JonB thanks a lot for your research on this ! Really appreciate your help on this.

                    First of all, one solution could be to get this thing working :
                    self.textedit.setStyleSheet(sheet)
                    But Im not really sure how to use it to define HTML new class to be honest, So ill have to dig deeper. Im used to use setStyleSheet for basic configs and formats, but not for custom Html Class.

                    I also did some testing on my own, but Im a bit disappointed.
                    I actually replaced My PlainTextedit with Textedit :

                    First of all, using a cursor and insertHtml does not give any satisfying result. It doesnt take into account my new class that I have loaded beforehand.

                    The only thing that seems to be working, is to keep using SetHtml which erases whole content and for that reason, I must keep stored in a variable my whole content as you stated indeed.
                    So basically, there is no clear advantage to use Textedit over PlainTextedit. With Plaintextedit, I can at least append without having to store the whole content, I just have to make sure the new class is defined everytime I want to use it.

                    I think the true solution is to look into QWebEngine , as many people told me to, from the beginning.
                    For now, I dont know anything about QWebEngine, so I'll just do some research on this.

                    JonBJ 1 Reply Last reply
                    0
                    • B Brad1111 has marked this topic as solved on
                    • B Brad1111

                      @JonB thanks a lot for your research on this ! Really appreciate your help on this.

                      First of all, one solution could be to get this thing working :
                      self.textedit.setStyleSheet(sheet)
                      But Im not really sure how to use it to define HTML new class to be honest, So ill have to dig deeper. Im used to use setStyleSheet for basic configs and formats, but not for custom Html Class.

                      I also did some testing on my own, but Im a bit disappointed.
                      I actually replaced My PlainTextedit with Textedit :

                      First of all, using a cursor and insertHtml does not give any satisfying result. It doesnt take into account my new class that I have loaded beforehand.

                      The only thing that seems to be working, is to keep using SetHtml which erases whole content and for that reason, I must keep stored in a variable my whole content as you stated indeed.
                      So basically, there is no clear advantage to use Textedit over PlainTextedit. With Plaintextedit, I can at least append without having to store the whole content, I just have to make sure the new class is defined everytime I want to use it.

                      I think the true solution is to look into QWebEngine , as many people told me to, from the beginning.
                      For now, I dont know anything about QWebEngine, so I'll just do some research on this.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #16

                      @Brad1111 said in Custom CSS style in Textedit:

                      With Plaintextedit, I can at least append without having to store the whole content

                      I did not find that. Per your finding, I found that calling QPlainTextEdit.appendHtml() a second time would not pick up the class from a previous call. Hence the code I wrote above works. So if you want to use QPlainTextEdit that is fine.

                      The only thing that seems to be working, is to keep using SetHtml which erases whole content and for that reason, I must keep stored in a variable my whole content as you stated indeed.

                      And that is what I would do.

                      You have solutions for both QTextEdit & QPlainTextEdit now. I don't know why you would bother moving over to QWebEngine for what you want. But if you want a full HTML stack that will do it.

                      B 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Brad1111 said in Custom CSS style in Textedit:

                        With Plaintextedit, I can at least append without having to store the whole content

                        I did not find that. Per your finding, I found that calling QPlainTextEdit.appendHtml() a second time would not pick up the class from a previous call. Hence the code I wrote above works. So if you want to use QPlainTextEdit that is fine.

                        The only thing that seems to be working, is to keep using SetHtml which erases whole content and for that reason, I must keep stored in a variable my whole content as you stated indeed.

                        And that is what I would do.

                        You have solutions for both QTextEdit & QPlainTextEdit now. I don't know why you would bother moving over to QWebEngine for what you want. But if you want a full HTML stack that will do it.

                        B Offline
                        B Offline
                        Brad1111
                        wrote on last edited by
                        #17

                        @JonB Id rather explore the QWebEngine mostly because, overall all the solutions found imply that we add the class definition each time we do sethtml .
                        But I could do this easily without any custom class right.
                        just using a long sequence of basic <></> before each message.
                        So in essence, the custom class does not really work that well. Not to mention inserthtml does not take the custom class either, and we dont have access to all html functions either.

                        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