Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Qt tooltip width

need to show 2 line text in tooltip with bold and multicolor characters, but seems tooltip has max width and text was cut. I tried to calculate width of text and set width manually, but it doesn't make any effects, seems style = "width: some px" doesn't work for tooltip. Here's the code:

Edited

QString tooltip = "<div style= \"white-space: nowrap; width: 1500px;\">Some text for tooltip, which is too long</div>";

there is no effect on tooltip

How can I change tooltip width?

like image 543
IKM2007 Avatar asked Jun 20 '13 18:06

IKM2007


2 Answers

Using the properties portion of QtCreator/Designer, I used:

<html>
<table width="25">
  <tr>
    <td width="25">alwkefjwekf</td>
  </tr>
  <tr>
    <td width="25">a ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea e</td>
  </tr>
</table>
</html>

This should help clue you in on how to better limit the size of the table. I have no doubt that there is probably a more terse way to express this.

A concrete example generated by QtCreator:

pushButton->setToolTip(QApplication::translate("MainWindow", "    <html>\n"
"    <table width=\"25\"><tr><td width=\"25\">alwkefjwekf</td></tr>\n"
"    <tr><td width=\"25\">a ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea e</td></tr>\n"
"    </table>\n"
"    </html>", 0));
like image 76
Huy Avatar answered Nov 07 '22 01:11

Huy


This is a little bit cleaner ...
Also, don't set "white-space: nowrap;", doing that with a small width will cut off text
And, when possible, don't hard code class names, since there is no __CLASS__ I used __FUNCTION__

If you really want the class name for Qt Linguist ( the translate function ) make sure the RTTI compiler option is on and use typeid(*this).name()

setToolTip( QApplication::translate(__FUNCTION__, "    <html>\n"
                                                  "    <div style=\"width: 300px;\">ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea</div>"
                                                  "    </html>", 0
                                 ));
like image 2
Lokist Avatar answered Nov 07 '22 01:11

Lokist