Setting the text colour of a tooltip in PyQt

2019-02-23 13:06发布

问题:

I've been adding tooltips to an app that I've been writing, and had issues with the colour of the tooltip text.

The app has a number of buttons, which change background and text colour depending on what the status of the button is. The text is either white or black.

The tooltip text colour always seems to follow the colour of the button text. When the text on the button is white, then it's all fine, but if the text on the button is black, then the tooltip is black text on a black background. What I want to be able to have is the tooltip to always be white text on black, whatever the button's colour.

For this, I was setting my tooltip using:

self.setToolTip(toolTip)

Now I did find a potential solution to this, but it had its own issues:

self.setToolTip("<font color=white>%s</font>" % toolTip.replace("\n", "<br/>"))

However, when I do this, the lines in my tooltip start wrapping, rather than keeping the full line length on one line, which is what I want it to do.

Any suggestions on either how to change the tooltip colour without using HTML, or how to get the HTML-based tooltip to not wrap?

Thanks

回答1:

You can use a stylesheet, for example:

self.setStyleSheet("""QToolTip { 
                           background-color: black; 
                           color: white; 
                           border: black solid 1px
                           }""")

There is more information available at:

http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qtooltip



标签: python qt4 pyqt4