From a9a4249b236f07418a3c678d3ec2b7791f957048 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 24 Oct 2020 15:35:57 +0200 Subject: 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 --- desktop-widgets/groupedlineedit.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'desktop-widgets') diff --git a/desktop-widgets/groupedlineedit.cpp b/desktop-widgets/groupedlineedit.cpp index fdce55ca4..5639b2e1b 100644 --- a/desktop-widgets/groupedlineedit.cpp +++ b/desktop-widgets/groupedlineedit.cpp @@ -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); -- cgit v1.2.3-70-g09d2