简体   繁体   中英

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>";

对工具提示没有影响

How can I change tooltip width?

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));

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
                                 ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM