I have a custom widget that inherits from Qlabel
which has a painEvent
member that plots a bunch of points, and I need to set a different tooltip for every point.
Here's a simplified standalone example of what I have:
from PyQt5.QtWidgets import QApplicationfrom PyQt5.QtCore import Qt, QPointfrom PyQt5.QtWidgets import QLabel, QSizePolicyfrom PyQt5.QtGui import QPixmap, QPainter, QPenclass MyWidget(QLabel): def __init__(self): super().__init__() self._pixmap = QPixmap() canvas = QPixmap(1020, 100) canvas.fill(Qt.white) self.setPixmap(canvas) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.point_data = [] def paintEvent(self, event): painter = QPainter(self) pen = QPen(Qt.black, 3) painter.setPen(pen) painter.drawLine(10, 50, 1000, 50) pen.setWidth(15) painter.setPen(pen) for p in self.point_data: point = QPoint(p['pos'], 50) point.setTooltip(p['info_str']) painter.drawPoint(point) painter.end()app = QApplication([])window = MyWidget()window.point_data = [ {'pos': 50, 'info_str': 'hello'}, {'pos': 200, 'info_str': 'goodbye'}]window.show()app.exec()
This doesn't plot any point and prints the error 'QPoint' object has no attribute 'setTooltip'
. If I remove the point.setTooltip(p['info_str'])
line it works.
So how do I add the tooltips?