PDA

View Full Version : Drawing on QTextEdit



jgrauman
6th February 2009, 01:42
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.


void JTextEdit::paintEvent(QPaintEvent *event)
{
QTextEdit::paintEvent(event);
QPainter painter(viewport());
QPen pe;
pe.setColor(Qt::red);
pe.setWidth(2);
painter.setPen(pe);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawLine(0,0,viewport()->maximumWidth()-1,viewport()->maximumHeight()-1);
}

fullmetalcoder
6th February 2009, 19:12
you got to take scrollbars into account when printing things in an abstract scroll area :


QAbstractScrollArea::horizontalScrollBar() (http://doc.trolltech.com/latest/qabstractscrollarea.html#horizontalScrollBar)
QAbstractScrollArea::verticallScrollBar() (http://doc.trolltech.com/latest/qabstractscrollarea.html#verticallScrollBar)

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 (http://qcodeedit.edyuk.org)or QScintilla (http://www.riverbankcomputing.co.uk/software/qscintilla/intro).

jgrauman
6th February 2009, 21:13
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

fullmetalcoder
7th February 2009, 09:40
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 :

painter.translate(-horizontalScrollBar()->value(), -verticalScrollBar()->value());

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.