PDA

View Full Version : How to change QToolTip color of a QPushbutton on Windows7



casestudy
21st December 2011, 18:36
Hello,

I would like to change tooltip both front and background color of a QPushButton object. My environment is
Windows7 Professional SP1 64bit, MSVC2008, Qt 4.7.2 (32bit).
However with below code, I only see the effect of QToolTip::setFont(), but not ::setBrush() or ::setColor(). I vaguely remember that QToolTip on Windows XP and Windows 7 behaves differently. On Windows XP, QToolTip background and front colors are the same as the parent QPushButton. On Windows 7, QToolTip background and front colors are uniform for all QPushButton's and cannot be modified. In my case, the background color is light gray with gradient. And front color is white. These results a very LOW contrast. Would you please confirm this? Or would you please hint me to how to change them? Thanks a lot.


QBrush brushToolTipBase(Qt::white);
QBrush brushToolTipText(Qt::red);
QPalette palette = QToolTip::palette();
palette.setBrush(QPalette::Inactive, QPalette::ToolTipBase, brushToolTipBase);
palette.setBrush(QPalette::Inactive, QPalette::ToolTipText, brushToolTipText);
palette.setColor(QPalette::Inactive, QPalette::ToolTipBase, Qt::white);
palette.setColor(QPalette::Inactive, QPalette::ToolTipText, Qt::red);
QToolTip::setPalette(palette);QFont serifFont("Times", 14, QFont::Bold);
QToolTip::setFont(serifFont);

Happy Holidays!
casestudy

Spitfire
22nd December 2011, 14:17
Take a look here (http://doc.qt.nokia.com/4.7-snapshot/stylesheet-examples.html#customizing-qtooltip), maybe it will help you.
If not you can always try to use rich text, for example:
QPushButton* pb = new QPushButton("test", this);
pb->setToolTip( "<span style=\"background-color:red;\">test</span>" );

casestudy
22nd December 2011, 16:21
Thanks a lot, Spitfire.
Both methods worked! However on Windows7, the QToolTip background is OS determined. I can only alter the front/text color with both methods. By using rich text format, you get the illusion that the background color can be altered. In reality, "<span style=\"background-color:red;\">test</span>" changes the color of each character's pixel block to red, *NOT* the background color of the QToolTip frame. Nevertheless, it fits my need.

It makes me wonder, why going through QPalette (as in my code example) would not work?!

Happy New Year and Thank you again!


Take a look here (http://doc.qt.nokia.com/4.7-snapshot/stylesheet-examples.html#customizing-qtooltip), maybe it will help you.
If not you can always try to use rich text, for example:
QPushButton* pb = new QPushButton("test", this);
pb->setToolTip( "<span style=\"background-color:red;\">test</span>" );