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 QApplication
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QLabel, QSizePolicy
from PyQt5.QtGui import QPixmap, QPainter, QPen
class 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?
The QPoint is an object that stores a position but is not associated with any graphic element. if you want to use multiple tooltips then you have to detect if the position you want was clicked and display the QToolTip. Something like: