Quantcast
Channel: Active questions tagged qpainter - Stack Overflow
Viewing all articles
Browse latest Browse all 58

How do I add tooltips to plot elements in PyQt5?

$
0
0

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?


Viewing all articles
Browse latest Browse all 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>