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:
- #include <QtGui>
- class Text: public QTextEdit
- Text(QWidget* pParent = 0);
- void paintEvent(QPaintEvent *event);
- // uncomment the following line to avoid crash
- // this->setWordWrapMode(QTextOption::NoWrap);
- this->setPlainText(" aaa ");
- void Text::paintEvent(QPaintEvent *event)
- QTextEdit::ExtraSelection highlight;
- QTextCursor originalCursor = this->textCursor();
- QTextCursor cursor = this->textCursor();
- // Mark current line
- highlight.cursor = this->textCursor();
- // Comment the following line to avoid crash
- highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
- extras << highlight;
- // Add extra selections
- // Comment the following line to avoid crash
- int main(int argc, char *argv)
- QApplication app(argc, argv);
- Text widget;
- return app.exec();
And finally links to the original bugs reported on our issue tracker:
Thanks in advance for any help you can offer.
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 [tug.org]). 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 have tested your code, and Creator warn about recursive repaint():
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 [bugreports.qt.nokia.com] including a backtrace and a proposed patch.