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. PyQT5 QTextEdit detects mouse over word when it's not
Forum Updated to NodeBB v4.3 + New Features

PyQT5 QTextEdit detects mouse over word when it's not

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 3 Posters 682 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.
  • Z Offline
    Z Offline
    ZeHgS
    wrote on last edited by
    #1

    Hi everyone!

    I am making a custom text editor and one of the functionalities is to highlight the line and word over which the mouse point is located. It worked perfectly in the sense that when the mouse really is over a word, it detects that word correctly. Here it is working perfectly in an image where my mouse pointer is located on top of the word "fortune":
    6c0b0401-d20a-4a69-9427-bb39e95b14af-image.png
    Here my pointer is below the text in the white area:
    60565046-6cdf-4ea9-bcce-1bcf01fba91d-image.png
    And here it is in the small amount of white space above the text, to the right of the topmost sentence:
    4e5b248d-8a3d-4c9c-ad78-95dd98e9038d-image.png
    In other words, the mouse cannot be inside the QTextEdit without being detected as being on top of something and it will default to the closest thing. Why is this happening? I don't think it's a problem in my code. Here is:
    def init(self, QTextEdit):
    self.setMouseTracking(True)

    def mouseMoveEvent(self, mouse_event: QMouseEvent) -> None:
        if self.underMouse():
            # get current mouse position
            position = mouse_event.pos()
    
            # create a QTextCursor at that position to select text
            text_cursor = self.cursorForPosition(position)
    
            # extract selected word
            text_cursor.select(QTextCursor.WordUnderCursor)
            word_under_cursor = text_cursor.selectedText()
            text_cursor.removeSelectedText()
            selected_word_placeholder = ''
            for char in word_under_cursor:
                selected_word_placeholder += 'ª'
            text_cursor.insertText(selected_word_placeholder)
    
            # extract selected sentence
            text_cursor.select(QTextCursor.LineUnderCursor)
            line_under_cursor = text_cursor.selectedText()
            text_cursor.removeSelectedText()
            text_cursor.insertText('SELECTED_SENTENCE_PLACEHOLDER')
    
            # replace newlines with a placeholder
            old_html = self.toHtml()
            no_newlines_html = old_html.replace('<br/>', 'PL_BR')
            no_newlines_html = no_newlines_html.replace('<br />', 'PL_BR2')
    
            # set the HTML so that we can use the QTextCursor
            self.setHtml(no_newlines_html)
    
            # convert the HTML to text to get rid of previous formatting
            soup = BeautifulSoup(self.toHtml(), 'lxml')
            as_text = soup.text
    
            # highlight the currently selected line
            open_highlight_tag = '<span style="background-color: #FFDDDD;">'
            close_highlight_tag = '</span>'
            highlighted_text = open_highlight_tag + line_under_cursor + close_highlight_tag
    
            bolded_selected_word = '<span style="background-color: #FFFF00;font-weight:bold">' + word_under_cursor + '</span>'
    
            # replace the placeholders
            sentence_placeholder_replaced = as_text.replace('SELECTED_SENTENCE_PLACEHOLDER', highlighted_text)
            word_placeholder_replaced = sentence_placeholder_replaced.replace(selected_word_placeholder, bolded_selected_word)
    
            final_html = word_placeholder_replaced.replace('PL_BR2', '<BR>').replace('PL_BR', '<BR>')
    
            # set HTML
            self.setHtml(final_html)
    
    eyllanescE 1 Reply Last reply
    0
    • Z ZeHgS

      Hi everyone!

      I am making a custom text editor and one of the functionalities is to highlight the line and word over which the mouse point is located. It worked perfectly in the sense that when the mouse really is over a word, it detects that word correctly. Here it is working perfectly in an image where my mouse pointer is located on top of the word "fortune":
      6c0b0401-d20a-4a69-9427-bb39e95b14af-image.png
      Here my pointer is below the text in the white area:
      60565046-6cdf-4ea9-bcce-1bcf01fba91d-image.png
      And here it is in the small amount of white space above the text, to the right of the topmost sentence:
      4e5b248d-8a3d-4c9c-ad78-95dd98e9038d-image.png
      In other words, the mouse cannot be inside the QTextEdit without being detected as being on top of something and it will default to the closest thing. Why is this happening? I don't think it's a problem in my code. Here is:
      def init(self, QTextEdit):
      self.setMouseTracking(True)

      def mouseMoveEvent(self, mouse_event: QMouseEvent) -> None:
          if self.underMouse():
              # get current mouse position
              position = mouse_event.pos()
      
              # create a QTextCursor at that position to select text
              text_cursor = self.cursorForPosition(position)
      
              # extract selected word
              text_cursor.select(QTextCursor.WordUnderCursor)
              word_under_cursor = text_cursor.selectedText()
              text_cursor.removeSelectedText()
              selected_word_placeholder = ''
              for char in word_under_cursor:
                  selected_word_placeholder += 'ª'
              text_cursor.insertText(selected_word_placeholder)
      
              # extract selected sentence
              text_cursor.select(QTextCursor.LineUnderCursor)
              line_under_cursor = text_cursor.selectedText()
              text_cursor.removeSelectedText()
              text_cursor.insertText('SELECTED_SENTENCE_PLACEHOLDER')
      
              # replace newlines with a placeholder
              old_html = self.toHtml()
              no_newlines_html = old_html.replace('<br/>', 'PL_BR')
              no_newlines_html = no_newlines_html.replace('<br />', 'PL_BR2')
      
              # set the HTML so that we can use the QTextCursor
              self.setHtml(no_newlines_html)
      
              # convert the HTML to text to get rid of previous formatting
              soup = BeautifulSoup(self.toHtml(), 'lxml')
              as_text = soup.text
      
              # highlight the currently selected line
              open_highlight_tag = '<span style="background-color: #FFDDDD;">'
              close_highlight_tag = '</span>'
              highlighted_text = open_highlight_tag + line_under_cursor + close_highlight_tag
      
              bolded_selected_word = '<span style="background-color: #FFFF00;font-weight:bold">' + word_under_cursor + '</span>'
      
              # replace the placeholders
              sentence_placeholder_replaced = as_text.replace('SELECTED_SENTENCE_PLACEHOLDER', highlighted_text)
              word_placeholder_replaced = sentence_placeholder_replaced.replace(selected_word_placeholder, bolded_selected_word)
      
              final_html = word_placeholder_replaced.replace('PL_BR2', '<BR>').replace('PL_BR', '<BR>')
      
              # set HTML
              self.setHtml(final_html)
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @ZeHgS I see that there is post-processing of the text where the text is modified, so to simplify the problem I ask myself: if this post-processing is eliminated, does the problem persist? You could provide a simplified and executable code of your problem.

      Another question I have is that let's say that everything works as expected, that is, that when the cursor is over a word then it is highlighted, and let's say that after I move the cursor to the white space below the text, do you have a logic that clears what highlighted?

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZeHgS
        wrote on last edited by
        #3

        Hello! Thank you so much for replying!

        Yes, the problem was there before the post-processing. The post-processing actually helped me visualize what was going on.

        I don't but the issue is that the text gets selected at all when the mouse enters from below, even if it's extremely distant from any text. What else could be causing this? I tried enabling mouse tracking on the parents as well.

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

          Hi and welcome to devnet,

          I don't have the solution at hand but what about detecting whether you're within the "text zone" and then only apply your logic if that's the case.

          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

          • Login

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