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. Finding a piece of text under cursor
Forum Updated to NodeBB v4.3 + New Features

Finding a piece of text under cursor

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.4k 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.
  • S Offline
    S Offline
    Shoto
    wrote on last edited by Shoto
    #1

    So I need to find a piece of text under cursor. Now here's the problem, I already solved it when the user clicks on a word but now I need to grab the word when the user hovers over the word. I'm using PyQt5 version 5.12.1 and python 3.7.

    The method I used to get the word under cursor then user clicked is this:
    Note: self is a class that inherits from QPlainTextEdit

    textCursor = self.textCursor()
    textCursor.select(QTextCursor.
    print(textCursor.selectedText())
    

    But now I wanna know how to get the same result without clicking

        def mouseMoveEvent(self, QMouseEvent):
    
            print(QMouseEvent.pos())
    
    

    My QPlainTextEdit class has mouse tracking turned on but I don't know how to proceed from here.

    Gojir4G 1 Reply Last reply
    0
    • S Shoto

      So I need to find a piece of text under cursor. Now here's the problem, I already solved it when the user clicks on a word but now I need to grab the word when the user hovers over the word. I'm using PyQt5 version 5.12.1 and python 3.7.

      The method I used to get the word under cursor then user clicked is this:
      Note: self is a class that inherits from QPlainTextEdit

      textCursor = self.textCursor()
      textCursor.select(QTextCursor.
      print(textCursor.selectedText())
      

      But now I wanna know how to get the same result without clicking

          def mouseMoveEvent(self, QMouseEvent):
      
              print(QMouseEvent.pos())
      
      

      My QPlainTextEdit class has mouse tracking turned on but I don't know how to proceed from here.

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @Shoto Hi,

      This may help to achieve your goal (code is C++):

      //in mouseMoveEvent
      QTextCursor cursor = cursorForPosition(event.pos());
      cursor.select(QTextCursor::WordUnderCursor);
      //Retrieve text with cursor.selectedText()
      
      S 1 Reply Last reply
      3
      • Gojir4G Gojir4

        @Shoto Hi,

        This may help to achieve your goal (code is C++):

        //in mouseMoveEvent
        QTextCursor cursor = cursorForPosition(event.pos());
        cursor.select(QTextCursor::WordUnderCursor);
        //Retrieve text with cursor.selectedText()
        
        S Offline
        S Offline
        Shoto
        wrote on last edited by
        #3

        @Gojir4 said in Finding a piece of text under cursor:

        cursorForPosition

        Thank you so much!

        Gojir4G 1 Reply Last reply
        0
        • S Shoto

          @Gojir4 said in Finding a piece of text under cursor:

          cursorForPosition

          Thank you so much!

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @Shoto Don't know for what you need that. But if this is for displaying a tooltip information about text under cursor, there is also another way to do it (sorry, C++ code again):

          bool MyTextEdit::event(QEvent *e)
          {
              if (e->type() == QEvent::ToolTip)
              {
                  QHelpEvent* helpEvent = static_cast<QHelpEvent*>(e);
                  QString txt = cursorForPosition(helpEvent.pos()).select(QTextCursor::WordUnderCursor);
                  if(txt.isEmpty())
                      return QPlainTextEdit::event(e);
          
                  //Display tooltip at cursor position
                  showToolTipForText(helpEvent->globalPos(), txt);
          
                  return true;
              }
              return QPlainTextEdit::event(e);
          }
          
          S 1 Reply Last reply
          1
          • Gojir4G Gojir4

            @Shoto Don't know for what you need that. But if this is for displaying a tooltip information about text under cursor, there is also another way to do it (sorry, C++ code again):

            bool MyTextEdit::event(QEvent *e)
            {
                if (e->type() == QEvent::ToolTip)
                {
                    QHelpEvent* helpEvent = static_cast<QHelpEvent*>(e);
                    QString txt = cursorForPosition(helpEvent.pos()).select(QTextCursor::WordUnderCursor);
                    if(txt.isEmpty())
                        return QPlainTextEdit::event(e);
            
                    //Display tooltip at cursor position
                    showToolTipForText(helpEvent->globalPos(), txt);
            
                    return true;
                }
                return QPlainTextEdit::event(e);
            }
            
            S Offline
            S Offline
            Shoto
            wrote on last edited by
            #5

            @Gojir4
            Hmm. I'm creating an IDE and C++ code is perfectly readable for me, thanks

            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