Results 1 to 4 of 4

Thread: Drawing on QTextEdit

  1. #1
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drawing on QTextEdit

    Hello,

    I'm trying to draw on a QTextEdit. I've been able to do so by subclassing QTextEdit and reimplementing the paintEvent function. However, when I scroll the QTextEdit, the line I drew doesn't scroll with it (which I need it to do). It doesn't just stay either, it gets messed up into a whole bunch of lines. Eventually I also want to be able to print the QTextEdit (with the lines).

    So I can see two ways of approaching it (maybe I'm wrong) but don't know how to do either.

    1) Draw on the QTextEdit widget. I would need to figure out how to make the lines scroll and also how to reimplement the print functions myself, since the print functions for QTextEdit operate on the QTextDocument class underlying the QTextEdit.

    2) Draw on a QPixmap and then inserting that into the QTextEdit/QTextDocument. Can images be inserted over the whole text area as foreground (preferably, but background could be ok) to the TextEdit?

    Any pointers would be appreciated. Thanks.

    Qt Code:
    1. void JTextEdit::paintEvent(QPaintEvent *event)
    2. {
    3. QTextEdit::paintEvent(event);
    4. QPainter painter(viewport());
    5. QPen pe;
    6. pe.setColor(Qt::red);
    7. pe.setWidth(2);
    8. painter.setPen(pe);
    9. painter.setRenderHint(QPainter::Antialiasing, true);
    10. painter.drawLine(0,0,viewport()->maximumWidth()-1,viewport()->maximumHeight()-1);
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to jgrauman for this useful post:

    Charlie37 (9th October 2009)

  3. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing on QTextEdit

    you got to take scrollbars into account when printing things in an abstract scroll area :


    Also it is probably a bad idea to try to draw text edit content by hand (or did I miss something?) unless you got very good reasons to not use QTextDocument but in such a case you would probably be better off with QCodeEdit or QScintilla.
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #3
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing on QTextEdit

    Thanks for the comments. As for painting on the QTextEdit, I want to be able to add effects that aren't part of QTextDocument (circling words and such) and hence want to be able to paint on the widget. Maybe there is a way to do this in a QTextDocument, but I'm not aware of any. Can it do overlays?

    Also, I'm not sure what you mean by "take scrollbars into account."

    Thanks.

    Josh

  5. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing on QTextEdit

    Quote Originally Posted by jgrauman View Post
    Can it do overlays?
    QTextDocument is great for rich text but it does not go beyond... circling and other complex formattings are out of its reach and there is no simple way to add them directly in QTextDocument. I suppose the only way to what you want is indeed custom drawing but it may get extremely complicated due to QTextDocument inherent complexity.

    Also, I'm not sure what you mean by "take scrollbars into account."
    Basically something like that :
    Qt Code:
    1. painter.translate(-horizontalScrollBar()->value(), -verticalScrollBar()->value());
    To copy to clipboard, switch view to plain text mode 

    Puzzled? I can understand for I had the same reaction when I first tried to reimplement QTextEdit drawing . This comes from the way scrolling is done in QAbstractScrollArea to reduce re-drawing overhead and cannot be easily worked around, even if you really wanted it.
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. QTextEdit Performance handling large paragraphs
    By netuno in forum Qt Programming
    Replies: 14
    Last Post: 1st September 2010, 22:58
  2. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18
  3. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  5. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40

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.