Crash when using QTextFormat::FullWidthSelection with word wrap
I'm one of the programmers of TeXworks, a free LaTeX editor which uses Qt for its interface. Recently, we had bug reports which I believe I have tracked down to the QTextFormat::FullWidthSelection flag we use to highlight the current line. I've opened a bug report for that (http://bugreports.qt.nokia.com/browse/QTBUG-16102), but it was closed immediately without any comments. So I'm trying my luck here - hopefully someone can help me.
Here's my original bug report:
If in a QTextEdit widget, one uses an extra selection with a QTextFormat::FullWidthSelection property (to highlight the current line; as suggested by the docs), and word wrapping is turned on, the application crashes (most of the time) when entering spaces at the end of a line so that the line becomes longer than the widget width (spaces do not seem to be wrapped, though). Apparently, a crash becomes more likely if there are non-whitespace characters close to the (right) edge of the widget. Note also that the cursor apparently must be positioned at the end of the line (i.e., outside the widget area) for the crash to happen. This only seems to affect Windows platforms.
And a minimal sample:
class Text: public QTextEdit
Text(QWidget* pParent = 0);
void paintEvent(QPaintEvent *event);
Text::Text(QWidget* pParent) : QTextEdit(pParent)
// uncomment the following line to avoid crash
this->setPlainText(" aaa ");
void Text::paintEvent(QPaintEvent event)
QTextCursor originalCursor = this->textCursor();
QTextCursor cursor = this->textCursor();
// Mark current line
highlight.cursor = this->textCursor();
// Comment the following line to avoid crash
extras << highlight;
// Add extra selections
// Comment the following line to avoid crash
int main(int argc, char *argv)
QApplication app(argc, argv);
And finally links to the original bugs reported on our issue tracker:
Thanks in advance for any help you can offer.
Unfortunately, you can not vote for closed bugs to get them open again by popular demand or something like that. It seems like you have a real issue on your hands. It is beyond me why the bugreport, with a testcase attached, would be closed without a comment.
I cannot reproduce a crash on Windows (VS 2008, Debug mode, Qt 4.7.1).
first of all thanks for the replies, and for testing.
I'm using Mingw with gcc 4.5.1 & 4.5.2 with Qt 4.7.1 (debug & release) on WinXP and Win7. The crash occurs reproducibly on my machine.
I experimented a bit more and the problem may be related only to the current cursor position. If I take the example code, the resulting line is slightly less wide than the window. If I resize the window so that the widget becomes narrower than the text (but there is no wrapping at the "aaa" yet), I can also produce the crash by moving the cursor into the area that is off-screen (i.e., without adding any characters). The crash only seems to occur when the cursor is close to the widget boundary (i.e., if I move the cursor to the end of a long line, no crash occurs; if I then start moving to the left, eventually the app crashes, presumably when the cursor is at the widget boundary). Again, this does not occur if I comment the use QTextFormat::FullWidthSelection.
Another useful note: When adding spaces, the crash only seems to occur when you do so "slowly" (wait, say, 1 second between adding consecutive spaces). I presume this has to do with the fact that the (usually blinking) cursor visibility must change for the crash to occur? Also, lines consisting only of spaces do not trigger the crash (hence the "aaa" in the example code).
BTW: Does anyone know why there doesn't seem to be a line wrap for spaces consisting only of spaces?
Thanks in advance for any further insight/help/comments you can offer.
this also seems to affect the script debugger bundled with Qt itself (see "http://tug.org/pipermail/texworks/2011q1/003647.html":http://tug.org/pipermail/texworks/2011q1/003647.html). So this seems to be a very serious issue.
Does anyone have additional ideas/suggestions/comments? Should I try again to open a bug report? Or is this issue known or even fixed in the current development code, perhaps (I didn't find anything in the bug tracker, but maybe I missed something)?
[EDIT: fixed link, Volker]
I would open a bug report. If you add your test case from above and instructions how to reproduce the error, it's more likely that it will be fixed sooner.
I have tested your code, and Creator warn about recursive repaint():
QWidget::repaint: Recursive repaint detected
QPaintDevice: Cannot destroy paint device that is being painted@
Debug your code and make sure the recursion is not infinite, also look in documentation which function calls repaint (probably one from the ones that if you comment solves the issue)
Thanks for all the helpful comments.
@Zlatomir: Unfortunately, I couldn't reproduce the warnings you got. At which point did they show up (compiling, running, debugging, after the crash,...)? FWIW, my configuration is Qt 4.7.1, mingw debug build with gdb debugger, on Win XP.
Using Qt Creator and gdb, I think I've been able to pinpoint the error. Consequently, I've opened a "new bug report":http://bugreports.qt.nokia.com/browse/QTBUG-17022 including a backtrace and a proposed patch.
I got that warnings at runtime in the Application Output, whenever the cursor is on right edge of the row and the window is resized over the cursor (as far that i understand this is how we should try to replicate the bug)
Using Qt SDK (tech preview) on Win 7 64bit, so no crash here, only warnings
OK, seems this bug has been fixed upstream in December.
Thanks for the help, guys.