Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QToolTip with different fonts



  • Hi,

    So i was wondering if it is possible to have the text of a QToolTip have multiple fonts. E.g the first line is bold and the second line is italic and so on.
    My assumption is that i have to call QToolTip::showText multiple times and set the font between the calls. However for this i have to change the position of the tooltip after each call and for that i would have to know the height of the tooltip box which i dont think is accessible. So how would i solve this?


  • Lifetime Qt Champion

    Hi,

    You can use rich text in your string see here.



  • Yes i already tried that but i am quite confused as to how this can be used. When using a QTextEdit (this is what the rich text link leads to) i still can only return a QString which is necessary due to the ToolTip function and that QString is n ot using different fonts. Maybe i am using the QTextEdit wrong too, im not sure.

        QFont f("Arial", 15, QFont::Bold);
        QFont g("Arial", 10, QFont::Bold);
        QTextEdit t;
        t.setFont(f);
        t.append("foo\n");
        t.setFont(g);
        t.append("bar");
    
        QToolTip::showText(somePos, t.toPlainText());
    

  • Lifetime Qt Champion

    As its name suggests toPlainText returns that: plain text, no formatting. You should get better result with toHtml.



  • Of course. Thank you, that worked.


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deem corrected so that other forum users may know a solution has been found :-)


Log in to reply