Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTextEdit: textBackgroundColor returns black if white
Forum Updated to NodeBB v4.3 + New Features

QTextEdit: textBackgroundColor returns black if white

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 545 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.
  • Fire CubeF Offline
    Fire CubeF Offline
    Fire Cube
    wrote on last edited by
    #1

    Hello Guys

    I want to get the background color of the selected text.
    This works good most of the time. But if the background color is white, it returns black.
    Black stays black. All the other colors works.

    Can you help me please?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please provide:

      • Version of PySide/PyQt
      • Version of Python
      • Method of installation
      • OS your are running
      • Minimal script that reproduce that behaviour

      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
      • Fire CubeF Offline
        Fire CubeF Offline
        Fire Cube
        wrote on last edited by
        #3

        PySide6 installed by pip
        Pyside6 version: 6.4.1
        Python version: 3.11.0
        OS: Windows 11

        Reproducer:

        from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit
        from PySide6.QtCore import QRect
        import sys
        
        class MainWindow(QMainWindow):
            def __init__(self):
                super().__init__()
        
                self.text_entry = QTextEdit(self)
        
                self.text_entry.setGeometry(QRect(0, 0, 917, 670))
                self.text_entry.setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\nhr { height: 1px; border-width: 0; }\nli.unchecked::marker { content: \"\\2610\"; }\nli.checked::marker { content: \"\\2612\"; }\n</style></head><body style=\" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#ffffff; background-color:#000000;\">I'm black</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif';\">I'm white</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#6cdfff; background-color:#ff8649;\">I'm colorful</span></p></body></html>")
                self.text_entry.selectionChanged.connect(self.on_selection_changed)
        
            def on_selection_changed(self):
                print(self.text_entry.textBackgroundColor().name())
        
        app = QApplication(sys.argv)
        
        window = MainWindow()
        window.show()
        
        sys.exit(app.exec())
        
        1 Reply Last reply
        0
        • Fire CubeF Offline
          Fire CubeF Offline
          Fire Cube
          wrote on last edited by
          #4

          I tried again, but still couldn't find a solution.
          I imagine this is a bug.

          1 Reply Last reply
          0
          • Ard_FlamingoA Offline
            Ard_FlamingoA Offline
            Ard_Flamingo
            wrote on last edited by Ard_Flamingo
            #5

            It's not a bug, you've actually never set the color or the background color for the white button. I've updated your code to add the background color and the color for the white button:

            from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit
            from PySide6.QtCore import QRect
            import sys
            
            class MainWindow(QMainWindow):
                def __init__(self):
                    super().__init__()
            
                    self.text_entry = QTextEdit(self)
            
                    self.text_entry.setGeometry(QRect(0, 0, 917, 670))
                    self.text_entry.setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\nhr { height: 1px; border-width: 0; }\nli.unchecked::marker { content: \"\\2610\"; }\nli.checked::marker { content: \"\\2612\"; }\n</style></head><body style=\" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#ffffff; background-color:#000000;\">I'm black</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#000000; background-color:#ffffff;\">I'm white</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#6cdfff; background-color:#ff8649;\">I'm colorful</span></p></body></html>")
                    self.text_entry.selectionChanged.connect(self.on_selection_changed)
            
                def on_selection_changed(self):
                    print(self.text_entry.textBackgroundColor().name())
            
            app = QApplication(sys.argv)
            
            window = MainWindow()
            window.show()
            
            sys.exit(app.exec())
            

            This updated code works perfectly on my end.

            In case you're wondering what I've done, I've just added:

            color:#000000; background-color:#ffffff;
            

            Before the code:

            \">I'm white</span></p>\n<p style=\
            
            1 Reply Last reply
            1

            • Login

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