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. Dynamic change of font color in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Dynamic change of font color in QTextEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 962 Views 1 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.
  • A Offline
    A Offline
    Anichka
    wrote on last edited by
    #1

    I want to change the color of font basing on the user input.
    More precisely, if a user gives a certain word, I want only this word to be red and leave the rest of text with the same color.

    I was thinking to interpret every given character and if the pattern matches, do the following:

    # to move the cursor to the beginning of given input pattern
    self.textEdit.moveCursor(QTextCursor.End - 3)
    responseStyle = "<span style=\"  color:" + RESPONSE_COLOR + ";\" >"
    # change the style
    self.textEdit.insertHtml(responseStyle)
    self.textEdit.moveCursor(QTextCursor.End)
    # end the changed style section
    self.textEdit.insertHtml("</span>")
    

    However, the color doesn't change.
    Does anyone have an idea how to solve it?

    JonBJ 1 Reply Last reply
    0
    • A Anichka

      I want to change the color of font basing on the user input.
      More precisely, if a user gives a certain word, I want only this word to be red and leave the rest of text with the same color.

      I was thinking to interpret every given character and if the pattern matches, do the following:

      # to move the cursor to the beginning of given input pattern
      self.textEdit.moveCursor(QTextCursor.End - 3)
      responseStyle = "<span style=\"  color:" + RESPONSE_COLOR + ";\" >"
      # change the style
      self.textEdit.insertHtml(responseStyle)
      self.textEdit.moveCursor(QTextCursor.End)
      # end the changed style section
      self.textEdit.insertHtml("</span>")
      

      However, the color doesn't change.
      Does anyone have an idea how to solve it?

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

      @Anichka
      Looks to me like it should work. Start by debugging out the whole of self.textEdit.text.toHtml() after the last line of your code so we can verify what is in there.

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

        Hi,

        Sounds a bit like you need QSyntaxHighlighter.

        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
        1

        • Login

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