我的覆盖方法toolTip()不调用。我该如何解决?
class MyCls(QGraphicsEllipseItem):
def __init__(self, x, y, r):
super().__init__(x, y, r, r)
self.setToolTip("Test")
def toolTip(self):
return "123"
答案 0 :(得分:0)
在您的示例中,我看不到您如何调用toolTip()
方法。
试试吧:
import sys
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtWidgets import (QGraphicsView, QGraphicsScene, QApplication,
QGraphicsEllipseItem, QGraphicsSceneHoverEvent,
QGraphicsSceneMouseEvent)
class MovableDisk(QGraphicsEllipseItem):
def __init__(self, top_left_x, top_left_y, radius):
super().__init__(0, 0, radius, radius)
self.setPos(top_left_x, top_left_y)
self.setBrush(Qt.red)
self.setAcceptHoverEvents(True)
self.setToolTip("Test") # < +++++
def hoverEnterEvent(self, event: 'QGraphicsSceneHoverEvent'):
QApplication.instance().setOverrideCursor(Qt.OpenHandCursor)
def hoverLeaveEvent(self, event: 'QGraphicsSceneHoverEvent'):
QApplication.instance().restoreOverrideCursor()
def mouseMoveEvent(self, event: 'QGraphicsSceneMouseEvent'):
new_cursor_position = event.scenePos()
old_cursor_position = event.lastScenePos()
old_top_left_corner = self.scenePos()
new_top_left_corner_x = new_cursor_position.x() - old_cursor_position.x() + old_top_left_corner.x()
new_top_left_corner_y = new_cursor_position.y() - old_cursor_position.y() + old_top_left_corner.y()
self.setPos(QPointF(new_top_left_corner_x, new_top_left_corner_y))
def mousePressEvent(self, event: 'QGraphicsSceneMouseEvent'): pass
def mouseDoubleClickEvent(self, event: 'QGraphicsSceneMouseEvent'): pass
def mouseReleaseEvent(self, event: 'QGraphicsSceneMouseEvent'):
self.setToolTip("<h3>pos: <hr>x({}), y({})</h3>" # < +++++
"".format(self.pos().x(), self.pos().y())) # < +++++
class MyView(QGraphicsView):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene()
self.setScene(self.scene)
self.setSceneRect(0, 0, 250, 250)
self.disk = MovableDisk(50, 50, 20)
self.scene.addItem(self.disk)
if __name__ == '__main__':
app = QApplication([])
f = MyView()
f.show()
sys.exit(app.exec_())