Custom painted line on QPlainTextEdit doesn't disappear
-
Hi guys, back from the dead!
I have a custom class that inherits from
QPlainTextEdit
. I overloaded thepaintEvent()
to draw a vertical line that indicates the 80 characters limit. I do it like this:void MyCodeEditor::paintEvent(QPaintEvent* event) { // Base class QPlainTextEdit::paintEvent(event); // Draw the ruler (vertical line) if (_rulerPositionInChars > 0 && _rulerPositionInPixels > 0) { QPainter painter(viewport()); painter.fillRect(_rulerPositionInPixels, 0, RULER_WIDTH, viewport()->height(), RULER_COLOR); painter.end(); } }
The
_rulerPositionInPixels
is an attribute of theMyCodeEditor
class which I set to the correct value inside of myMyCodeEditor::setRulerPosition(int numberOfChars)
method. At the end of that method I also callupdate()
. Now, depending on the parameter passed toMyCodeEditor::setRulerPosition()
it can happen that the line should no longer be drawn at all. I give the user the ability to dynamically toggle the visibility of the line with aQAction
inside aQToolbar
above the code editor widget.The problem I'm experiencing is that when I set the visibility of the line to
false
which results in thepaintEvent()
not painting the line, the lines doesn't disappear until there's new text loaded. But even then, only the blocks (in terms ofQTextDocument
) that were refreshed erase the line so that sometimes there are just fragments / segments left.My question: What do I have to do to make the line genuinely disappear? I tried using
QWidget::repaint()
instead ofQWidget::update()
but with no luck.
I thought that the call the the base classQPlainTextEdit::paintEvent()
would take care of clearing the widget area and thus removing the preivously drawn line.Any ideas?
-
Welcome back :)
-
Hi! Just add
viewport()->update();
to yourpaintEvent()
override. -
Oh... well that was stupid...
Thank you very much, kind Sir. Much appreciated! -
:) Btw, you don't have to call
painter.end()
; it's automatically called when painter gets destroyed. -
Thanks for the remark, I appreciate it!