PyQt - QToolTip



QToolTip is a component of PyQt that allows developers to provide contextual information about various elements within their graphical user interfaces. These tooltips are displayed when the user hovers over an item, offering brief descriptions, helpful hints, or additional information.

Basic Usage of Tooltip

Integrating tooltips into PyQt applications is straightforward. We can set tooltips for any widget that inherits from QWidget using the setToolTip() method. Here's a basic example −

In the below example, when we hover over the QPushButton, it will display the tooltip "This is a QPushButton."

import sys
from PyQt6.QtWidgets import QApplication, QPushButton

def main():
   app = QApplication(sys.argv)

   button = QPushButton('Hover Me')
   button.setToolTip('This is a QPushButton')

   button.show()
   sys.exit(app.exec())

if __name__ == '__main__':
   main()

Output

pyqt tooltip basic usage

Advanced Usage

QToolTip offers various customization options, including adjusting the tooltip's font, background color, and timing of display. We can use this customization features by subclassing QToolTip and overriding its default behavior.

In the below example, we use the customization feature of tooltip to customize the font of the tooltip to Arial with a size of 12.

import sys
from PyQt6.QtWidgets import QApplication, QPushButton, QToolTip
from PyQt6.QtGui import QFont

def main():
   app = QApplication(sys.argv)

   button = QPushButton('Hover Me')
   button.setToolTip('This is a QPushButton')

   QToolTip.setFont(QFont('Arial', 12))

   button.show()
   sys.exit(app.exec())

if __name__ == '__main__':
   main()

Output

pyqt tooltip advance usage

Methods used in QTooltip

Method Description
setToolTip() Sets the tooltip for a widget.
setFont() Sets the font used for tooltips.
setPalette() Sets the palette used for tooltips, including background and foreground colors.
showText() Displays a tooltip with specified text.
hideText() Hides the currently displayed tooltip.

Example 1: Tooltip with Image

In this example, we will create a tooltip which will displays a tooltip with an image when hovering over the button.

import sys
from PyQt6.QtWidgets import QApplication, QPushButton, QToolTip
from PyQt6.QtGui import QFont, QPixmap

def main():
   app = QApplication(sys.argv)

   button = QPushButton('Hover Me')
   button.setToolTip('')

   button.show()
   sys.exit(app.exec())

if __name__ == '__main__':
   main()

Output

The above code produces the following output −

pyqt tooltip example 1

Example 2: Tooltip with HTML Formatting

In the below example, the tooltip text is formatted using HTML, displaying a blue-colored header.

import sys
from PyQt6.QtWidgets import QApplication, QPushButton, QToolTip

def main():
   app = QApplication(sys.argv)

   button = QPushButton('Hover Me')
   button.setToolTip('

This is a QPushButton

') button.show() sys.exit(app.exec()) if __name__ == '__main__': main()

Output

The above code produces the following output −

pyqt tooltip example 2

Example 3: Custom Tooltip Duration

In this example, the tooltip is displayed for 3 seconds after hovering over the button.

import sys
from PyQt6.QtWidgets import QApplication, QPushButton, QToolTip
from PyQt6.QtCore import QTimer

def main():
   app = QApplication(sys.argv)

   button = QPushButton('Hover Me')
   button.setToolTip('This is a QPushButton')

   timer = QTimer()
   timer.setSingleShot(True)
   timer.timeout.connect(lambda: button.setToolTip(''))
   button.enterEvent = lambda event: timer.start(3000)

   button.show()
   sys.exit(app.exec())

if __name__ == '__main__':
   main()

Output

The above code produces the following output −

pyqt tooltip example 3
Advertisements