Results 1 to 2 of 2

Thread: coordinates of selected text in QTextEdit

  1. #1
    Join Date
    Feb 2006
    Posts
    60
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default coordinates of selected text in QTextEdit

    hi,
    I want to get the cooordinates of the selected text in QTextEdit, any one corner of the of the selected text.

    Please post code snippet if possible

    Thank you,

    regards,
    sreedhar

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: coordinates of selected text in QTextEdit

    This might be a little complicated (and it hasn't been tested)...

    Try using QTextEdit::cursorRect(). If we assume that the cursor always touches one of the ends of the selection, you'll get your coordinates correctly.

    If it doesn't, you can retrieve the position of the selection using QTextCursor::selectionStart() (or selectionEnd), then get a QTextBlock object using QTextDocument::findBlock(). Then get the layout using QTextBlock::layout() which has a QTextLayout::boundingRect() method, which will give you the coordinates of the block or QTextLayout::position() which will return the exact point.

    These coordinates should be correct when taken as the viewport coordinates of the text edit widget.

    But as I said -- no warranty this will work. It's just a logical chain of calls. It might result in something completely different.

  3. The following user says thank you to wysota for this useful post:

    sreedhar (7th June 2006)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Replies: 2
    Last Post: 30th December 2008, 21:35
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.