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. a textedit text color problem
Forum Updated to NodeBB v4.3 + New Features

a textedit text color problem

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 308 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
    lfmissonb
    wrote on last edited by
    #1

    When I append text1 with green color, and select the green text, then append text2 without <font> label, but text2 become green too:
    Step I: click "add text green color"
    click "add text green color"

    Step II: select "green text"
    select "green text"

    Step III: click "add text unset color"
    click "add text unset color

    If I don't select the green text1 to append text2, text2 will be white (or black in light theme):
    Step I: click "add text green color"
    Step I: click "add text green color"

    Step II: click "add text unset color"
    click "add text unset color"

    PySide6(Qt6 for Python): 6.7.2
    My Code:

    import sys
    from PySide6 import QtWidgets
    
    app = QtWidgets.QApplication(sys.argv)
    
    win = QtWidgets.QMainWindow()
    central = QtWidgets.QWidget(win)
    win.setCentralWidget(central)
    layout = QtWidgets.QVBoxLayout(central)
    central.setLayout(layout)
    
    text_edit = QtWidgets.QTextEdit(central)
    layout.addWidget(text_edit)
    
    add_normal_text_btn = QtWidgets.QPushButton(central)
    add_normal_text_btn.setText("add text unset color")
    add_normal_text_btn.clicked.connect(lambda: text_edit.append("normal text"))
    layout.addWidget(add_normal_text_btn)
    
    add_green_text_btn = QtWidgets.QPushButton(central)
    add_green_text_btn.setText("add text green color")
    add_green_text_btn.clicked.connect(lambda: text_edit.append("<font color=\"green\">green text</font>"))
    layout.addWidget(add_green_text_btn)
    
    win.show()
    
    sys.exit(app.exec())
    
    JonBJ 1 Reply Last reply
    0
    • L lfmissonb

      When I append text1 with green color, and select the green text, then append text2 without <font> label, but text2 become green too:
      Step I: click "add text green color"
      click "add text green color"

      Step II: select "green text"
      select "green text"

      Step III: click "add text unset color"
      click "add text unset color

      If I don't select the green text1 to append text2, text2 will be white (or black in light theme):
      Step I: click "add text green color"
      Step I: click "add text green color"

      Step II: click "add text unset color"
      click "add text unset color"

      PySide6(Qt6 for Python): 6.7.2
      My Code:

      import sys
      from PySide6 import QtWidgets
      
      app = QtWidgets.QApplication(sys.argv)
      
      win = QtWidgets.QMainWindow()
      central = QtWidgets.QWidget(win)
      win.setCentralWidget(central)
      layout = QtWidgets.QVBoxLayout(central)
      central.setLayout(layout)
      
      text_edit = QtWidgets.QTextEdit(central)
      layout.addWidget(text_edit)
      
      add_normal_text_btn = QtWidgets.QPushButton(central)
      add_normal_text_btn.setText("add text unset color")
      add_normal_text_btn.clicked.connect(lambda: text_edit.append("normal text"))
      layout.addWidget(add_normal_text_btn)
      
      add_green_text_btn = QtWidgets.QPushButton(central)
      add_green_text_btn.setText("add text green color")
      add_green_text_btn.clicked.connect(lambda: text_edit.append("<font color=\"green\">green text</font>"))
      layout.addWidget(add_green_text_btn)
      
      win.show()
      
      sys.exit(app.exec())
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @lfmissonb
      I can only guess/suggest that when you append your "unset color text" (which btw does not unset any color, perhaps it should) the QTextEdit/QTextDocument makes a guess whether to continue from existing selected text or not. So you get what you get. I suspect the Qt folks will say this is "by design" or "unspecified", so you will need to experiment with what works/does not work when it updates the HTML to get what you want.

      L 1 Reply Last reply
      0
      • JonBJ JonB

        @lfmissonb
        I can only guess/suggest that when you append your "unset color text" (which btw does not unset any color, perhaps it should) the QTextEdit/QTextDocument makes a guess whether to continue from existing selected text or not. So you get what you get. I suspect the Qt folks will say this is "by design" or "unspecified", so you will need to experiment with what works/does not work when it updates the HTML to get what you want.

        L Offline
        L Offline
        lfmissonb
        wrote on last edited by
        #3

        @JonB
        Thank you. I found that I shouldn't only append "normal text", I should append "<font>normal text</font>" instead, and the "normal text" will not be green.

        JonBJ 1 Reply Last reply
        0
        • L lfmissonb has marked this topic as solved on
        • L lfmissonb

          @JonB
          Thank you. I found that I shouldn't only append "normal text", I should append "<font>normal text</font>" instead, and the "normal text" will not be green.

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

          @lfmissonb Indeed, because that specifies its own font (and so color) for the fragment you are adding.

          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