2

How do I get the font size and color of the text in the tooltip to change from that of the button? It keeps displaying as the size/font of the pushbutton instead of it's own. Also how do I get the text "leave the program" to actually fit in the tooltip?

I've tried this method and couldn't get it to work: Setting the text colour of a tooltip in PyQt

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *  

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(100, 100)
        self.setWindowTitle("Example")

        self.leave = QPushButton("X", self)
        self.leave.setStyleSheet("background-color : grey ; color : red ; font: 20pt")
        self.leave.setToolTip("Leave the Program")
        self.setStyleSheet("QToolTip{background-color : blue ; color: k ; font: 12pt}")
        self.leave.move(38, 25)
        self.leave.resize(24, 50)
        self.leave.clicked.connect(self.exit)

    def exit(self):
        app.quit()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow() 
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
alex25
  • 23
  • 3

1 Answers1

4

The tool tip in question is a child of the button not of the main window so it will inherit the style sheet of the button (as well as those of the button's ancestors). Since you didn't specify a selector in the style sheet of the button, the style rules in this style sheet will be applied to all the button's children including the tool tip (unless they have a style sheet themselves). One way around this is to limit the button's style sheet to QPushButton objects only by doing something like

self.leave.setStyleSheet("QPushButton{background-color : grey ; color : red ; font: 20pt}")

Furthermore, to get the background color of the tool tip to change from default I needed to specify a style rule for its border, e.g.

self.setStyleSheet(" QToolTip{ border: 1px solid white; background-color: blue ; color: k ; font: 12pt}")

I am not sure if this is a bug or by design.

Screenshot:

Screenshot of tool tip

Heike
  • 24,102
  • 2
  • 31
  • 45