desktop: avoid crashes on drag&drop in GroupedLineEdit

If the user manages to "scroll" through the QPlainTextEdit by
a drag&drop action, the state of the widget becomes inconsistent.
On the one hand, the text-block says that it has one line.
On the other hand, its layout says that it has no line.
When trying to fetch the line, a crash occurs.

Try to detect such a strange state and return early in
GroupedLineEdit::paintEvent().

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-10-24 15:35:57 +02:00 committed by Dirk Hohndel
parent 0278f3f90c
commit a9a4249b23

View file

@ -161,7 +161,15 @@ void GroupedLineEdit::keyPressEvent(QKeyEvent *e)
void GroupedLineEdit::paintEvent(QPaintEvent *e)
{
QTextLine line = document()->findBlock(0).layout()->lineAt(0);
// Do some sanity checks. Without these, the code may crash if
// the user manages to scroll the text box by drag&drop actions.
const QTextBlock &block = document()->findBlock(0);
if (block.lineCount() <= 0)
return QPlainTextEdit::paintEvent(e);
const QTextLayout *layout = block.layout();
if (layout->lineCount() <= 0)
return QPlainTextEdit::paintEvent(e);
QTextLine line = layout->lineAt(0);
QPainter painter(viewport());
painter.setRenderHint(QPainter::Antialiasing, true);