Results 1 to 6 of 6

Thread: QTextEdit::cursorForPosition() and character at mouse pointer?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2011
    Posts
    44
    Qt products
    Qt4
    Platforms
    MacOS X Windows Symbian S60
    Thanked 13 Times in 11 Posts

    Default Re: QTextEdit::cursorForPosition() and character at mouse pointer?

    Try the following:
    Qt Code:
    1. // m_Edit - QTextEdit object
    2. // ptViewport - mouse position in viewport coordinates
    3. // docLPos - position of character under mouse
    4. int docLPos = m_Edit->document()->documentLayout()->hitTest( QPointF( ptViewport.x(), ptViewport.y() ), Qt::ExactHit );
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to llev for this useful post:

    GrayOwl (13th March 2021), miwarre (4th February 2012)

  3. #2
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    7
    Thanked 6 Times in 6 Posts

    Default Re: QTextEdit::cursorForPosition() and character at mouse pointer?

    Quote Originally Posted by llev View Post
    Try the following:
    Qt Code:
    1. int docLPos = m_Edit->document()->documentLayout()->hitTest( QPointF( ptViewport.x(), ptViewport.y() ), Qt::ExactHit );
    To copy to clipboard, switch view to plain text mode 
    Thanks for the reply and my apologies for the delay in acknowledging it: the project has been suspended and I completely forgot about this pending issue.

    I was aware of the hitTest() function, but I assumed it was working the same as cursorForPosition() (oh, never, never assume anything!), so I did not even tried it.

    When finally answering your post, in order to give a complete reply as possible, I tried it and, lo!, it works indeed!.

    So, thanks for overriding my stubborness!

    M.

  4. #3
    Join Date
    Mar 2021
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QTextEdit::cursorForPosition() and character at mouse pointer?

    Quote Originally Posted by llev View Post
    Try the following:
    Qt Code:
    1. // m_Edit - QTextEdit object
    2. // ptViewport - mouse position in viewport coordinates
    3. // docLPos - position of character under mouse
    4. int docLPos = m_Edit->document()->documentLayout()->hitTest( QPointF( ptViewport.x(), ptViewport.y() ), Qt::ExactHit );
    To copy to clipboard, switch view to plain text mode 
    While having the same pb, i tried your solution and it works like a charm thanks

Similar Threads

  1. tab character in QTextEdit
    By johnmauer in forum Qt Programming
    Replies: 0
    Last Post: 1st November 2010, 16:17
  2. QTextEdit End-of-Line character
    By smhall316 in forum Newbie
    Replies: 1
    Last Post: 29th September 2010, 18:28
  3. QTextEdit: get word under the mouse pointer?
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 13th November 2009, 15:42
  4. How to get current character from QTextEdit
    By iamjayanth in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2009, 08:05
  5. QTextEdit and Form Feed character
    By gvlaovic in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2007, 12:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.