Jochen Schwarze
Jochen Schwarze

Reputation: 292

QMessageBox avoid auto linebreaks

Is there a way to restrict linebreaks in the text of a QMessageBox to where a <br> occurs? I.e. no auto-linebreaks anywhere else? Something like msgBox.setAutoLinebreaks(False) would be nice...

Upvotes: 1

Views: 1295

Answers (1)

Sergei Tachenov
Sergei Tachenov

Reputation: 24879

If you really want, you may use this dirty hack to do it:

QMessageBox msgBox;
QString loremIpsum = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
loremIpsum.replace(" ", "&nbsp;");
msgBox.setText(loremIpsum);
msgBox.setTextFormat(Qt::RichText);
msgBox.exec();

But the size of the message box is still limited by some internal logic, so it simply won't display the rest of the line if it doesn't fit. On my system this is truncated at “ab ill”. Note that auto-breaking algorithm might break not only at spaces (not sure about that), so you may have to use non-breaking hyphens or something more. I wouldn't dare to use this as a generic solution, but it could be used for a specific case.

Upvotes: 1

Related Questions