Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to wordwrap text in QToolTip than just using RegExp?

Tags:

c++

qt

So the question is in the title. QToolTip doesn't seem to provide the wordwraop feature. It is possible, however, to replace the Nth space with an \n using regexps, but I was wondering if anyone has a suggestion for a better sollution. Specifically, my problem with my approach is that it doesn't take the length of text into account. For example I'd like longer texts to form wider paragraphs.

like image 313
ravil Avatar asked Jan 25 '11 16:01

ravil


2 Answers

If the text in a tooltip is rich text, it is automatically word-wrapped.

Here's a trivial example, where setting the font to black makes it "rich text" and so it gets word wrapped. Leaving out the font declarations means the tooltip will be plain text and extend the whole length of the screen.

QString toolTip = QString("<FONT COLOR=black>");
toolTip += ("I am the very model of a modern major general, I've information vegetable animal and mineral, I know the kinges of England and I quote the fights historical from Marathon to Waterloo in order categorical...");
toolTip += QString("</FONT>");
widget->setToolTip(sToolTip);

Of course with this example, the width of the tooltip is up to the platform.

There is a new suggestion in the Qt bug tracker about this problem: https://bugreports.qt.io/browse/QTBUG-41051. It also requests width to be changeable.

like image 165
dabhaid Avatar answered Sep 21 '22 12:09

dabhaid


Alternate answer: use <nobr> at the beginning of your tooltip to specify a section of text that will define the width. For example:

<nobr>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed</nobr>
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.

The text between <nobr> and </nobr> will force a minimum width. The rest will wrap accordingly. (This is helpful if you're not satisfied with the default width used by Qt, which in my experience tends to be too narrow.)

like image 26
Matthew Avatar answered Sep 20 '22 12:09

Matthew