Results 1 to 4 of 4

Thread: How to draw a rectangle around selected text in QPlainTextEditor in Qt?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Posts
    227
    Thanks
    139
    Thanked 6 Times in 5 Posts

    Exclamation How to draw a rectangle around selected text in QPlainTextEditor in Qt?

    Idea here is to let the user select as much text he wants in the text editor, then have a rectangle displayed around that selected area.

    This rectangle has to take the shape of the selected text.

    I found blockBoundingRect function which returns the bounding rectangle of the QTextBlock in the QTextBlock's own coordinates.

    I am not sure if this would be of some help. What do I need to do to display the rectangle around the selected text?

  2. #2
    Join Date
    Apr 2011
    Posts
    227
    Thanks
    139
    Thanked 6 Times in 5 Posts

    Default Re: How to draw a rectangle around selected text in QPlainTextEditor in Qt?

    I have discovered QRubberBand class. I don't know how to use it though, yet.
    https://doc.qt.io/qt-5/qrubberband.html

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,714
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to draw a rectangle around selected text in QPlainTextEditor in Qt?

    I don't know how to use it though, yet.
    I don't think you want to use it for this purpose, though. A rubberband is generally used when selecting something - it has a starting anchor point that is established on a mouse press event, and then stretches as a line or a rectangle during mouse move events, and then is erased upon a mouse release.

    In order to draw your rectangle on the text edit, you will probably have to derive a class from QPlainTextEdit and override the paintEvent(). Call the base class paintEvent() first, then use the QPainter to draw your rectangle.

    You will have to evaluate the return value from the bounding rect for the QTextBlock to see if the coordinates make sense. If the coordinates seem to match up with the position of the text within the view, then just use them to draw your rect. If not, you may have to map them in some way to widget coordinates.

    I am not familiar enough with QTextDocument to know how QTextBlock works. I don't know if a block has to align with paragraph boundaries of whether it can start and/or end within a paragraph. In addition, your user might select text that is only partly contained within a block, spans more than one block, or is in the interior of the block. In those cases, drawing a rectangle based on block coordinates would give a misleading representation of what the user had selected.
    Last edited by d_stranz; 5th September 2020 at 22:50.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    TheIndependentAquarius (6th September 2020)

  5. #4
    Join Date
    Apr 2011
    Posts
    227
    Thanks
    139
    Thanked 6 Times in 5 Posts

    Default Re: How to draw a rectangle around selected text in QPlainTextEditor in Qt?

    Thank you for the explanation.

Similar Threads

  1. Replies: 1
    Last Post: 3rd June 2014, 12:23
  2. Tree View Item selected rectangle
    By LynneV in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2010, 17:17
  3. How change selected item rectangle on QGraphicsScene
    By xray2000 in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2010, 17:43
  4. Replies: 10
    Last Post: 25th June 2009, 03:09
  5. how to zoomin the selected rectangle?
    By lengshuang in forum Qt Programming
    Replies: 0
    Last Post: 25th April 2009, 03:42

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.