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

Dynamic change of font color in QTextEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 949 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.
  • A Offline
    A Offline
    Anichka
    wrote on 13 Jan 2021, 07:16 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?

    J 1 Reply Last reply 13 Jan 2021, 08:19
    0
    • A Anichka
      13 Jan 2021, 07:16

      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?

      J Offline
      J Offline
      JonB
      wrote on 13 Jan 2021, 08:19 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 13 Jan 2021, 19:21 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

        2/3

        13 Jan 2021, 08:19

        • Login

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