Changing a single strings color within a QTextEdit
Asked Answered
B

5

11

I am working on a GUI developed via PyQt and Qt4. Within my GUI I have a QTextEdit that has various data written to. Is there a way in which I can manipulate the color of one word within the QTextEdit?

For example

redText = "I want this text red"
self.myTextEdit.write(redText)
blackText = "And this text black"
self.myTextEdit.append(blackText)

Is this possible? If so, how could I do this?

Regards,

sudo!!

Backstage answered 18/6, 2014 at 13:39 Comment(0)
I
12

You should provide a rich text for it. It can be done by creating a <span> tag and setting the color property to an RGB value :

redText = "<span style=\" font-size:8pt; font-weight:600; color:#ff0000;\" >"
redText.append("I want this text red")
redText.append("</span>")
self.myTextEdit.write(redText)

blackText = "<span style=\" font-size:8pt; font-weight:600; color:#000000;\" >"
blackText.append("And this text black")
blackText.append("</span>")
self.myTextEdit.append(blackText)
Ineligible answered 18/6, 2014 at 13:45 Comment(0)
B
8

After some research into other methods people have used, I figured it out and wanted to share. I tried the ".setHtml" function with the QTextEdit, but it didn't work.

I figured out you can change the text color, add your text and then change it again and any text that's added after you've changed the color turns to that color, but nothing else.

Here's an example.

redColor = QColor(255, 0, 0)
blackColor = QColor(0, 0, 0)

# First, set the text color to red    
self.myTextEdit.setTextColor(redColor)

redText = "I want this text red"
self.myTextEdit.write(redText)

# To change it back to black, we manually use `setTextColor` again
self.myTextEdit.setTextColor(blackColor)

blackText = "And this text black"
self.myTextEdit.append(blackText)

And also, I want to add. ".write" and ".append" functions don't work for my "QTextEdit" class. Not sure if yours do, but what worked for me was the ".insertPlainText" function. Just convert your string to a "QString" like so

blackText = QString(blackText)
Brownson answered 27/10, 2017 at 17:18 Comment(0)
H
1

The answer of Nejat works for me by replacing ".append()" with "+=" :

redText = "<span style=\" font-size:8pt; font-weight:600; color:#ff0000;\" >"
redText += "I want this text red"
redText += "</span>"
self.myTextEdit.write(redText)

blackText = "<span style=\" font-size:8pt; font-weight:600; color:#000000;\" >"
blackText += "And this text black")
blackText += "</span>"
self.myTextEdit.append(blackText)
Habitude answered 12/7, 2019 at 8:12 Comment(0)
T
0

I faced the same problem and I did not find a clear solution to solve it. Basically, my GUI, before figuring out how to color the text the way it works, overlapped the colors and was unable to work with texts using independent colors.

So, behold, one day browsing the internet I gathered some information and discovered something like:

        #Import QColor, this will be responsible for doing the job.
        from PyQt5.QtGui import QColor
        from PyQt5 import uic, QtWidgets

        class Program:
            def writeonthescreen(self):
                #Set a color
                Screen.your_text_edit.setTextColor(QColor(255, 51, 0))
                
                #Write colored text
                Screen.your_text_edit.append('Red')

                Screen.your_text_edit.setTextColor(QColor(0, 204, 0))
                        
                Screen.your_text_edit.append('Green')

                Screen.your_tex_edit.setTextColor(QColor(0, 0, 255))
                        
                Screen.your_text_edit.append('Blue')


        if __name__ == '__main__':
            '''
            "Screen" is the name we will use to name the screen to be loaded.
             Imagine that this screen contains a QTextEdit, and a button that when pressed displays your text.

            '''
            app = QtWidgets.QApplication([])
            Screen = uic.loadUi('./your_screen_path')
            Screen.button_showtext_on_the_screen.clicked.connect(Program.writeonthescreen)
            
            Screen.show()
            app.exec()
Tensity answered 15/5, 2021 at 0:8 Comment(0)
R
0

PySide is pretty similar to PyQt so if anyone comes wondering around this searching for PySide this code will work for PySide6

red_text = "I want this text red"
self.myTextEdit.setHtml(f"<span style=\"color:#ff0000;\" > {red_text} </span>")
black_text = "I want this text black"
self.myTextEdit.setHtml(f"<span style=\"color:#000000;\" > {black_text} </span>")
Ringlet answered 13/8, 2021 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.