在我的应用程序中,我有一个QGraphicsScene,用户应该可以通过鼠标按钮单击并悬停在项目上来更改项目的颜色。单击PyQt时悬停事件
下面是一个例子代码,这是我从另一个问题借来的:
PyQt: hover and click events for graphicscene ellipse
from PyQt5 import QtGui, QtCore, QtWidgets
class MyFrame(QtWidgets.QGraphicsView):
def __init__(self, parent = None):
super(MyFrame, self).__init__(parent)
self.setScene(QtWidgets.QGraphicsScene())
# add some items
x = 0
y = 0
w = 15
h = 15
pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
brush = QtGui.QBrush(pen.color().darker(150))
# i want a mouse over and mouse click event for this ellipse
for xi in range(3):
for yi in range(3):
item = callbackRect(x+xi*30, y+yi*30, w, h)
item.setAcceptHoverEvents(True)
item.setPen(pen)
item.setBrush(brush)
self.scene().addItem(item)
item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
class callbackRect(QtWidgets.QGraphicsRectItem):
'''
Rectangle call-back class.
'''
def mouseReleaseEvent(self, event):
# recolor on click
color = QtGui.QColor(180, 174, 185)
brush = QtGui.QBrush(color)
QtWidgets.QGraphicsRectItem.setBrush(self, brush)
return QtWidgets.QGraphicsRectItem.mouseReleaseEvent(self, event)
def hoverMoveEvent(self, event):
# Do your stuff here.
pass
def hoverEnterEvent(self, event):
color = QtGui.QColor(0, 174, 185)
brush = QtGui.QBrush(color)
QtWidgets.QGraphicsRectItem.setBrush(self, brush)
def hoverLeaveEvent(self, event):
color = QtGui.QColor(QtCore.Qt.green)
brush = QtGui.QBrush(color.darker(150))
QtWidgets.QGraphicsRectItem.setBrush(self, brush)
if (__name__ == '__main__'):
app = QtWidgets.QApplication([])
f = MyFrame()
f.show()
app.exec_()
因此,在这种代码的悬停方法只有当没有按下鼠标键调用。如文档中所述(用于PySide),mousePressEvent“决定它接收鼠标事件的图形项目”,它以某种方式阻止其他项目的鼠标事件。
但是,有没有办法同时按住鼠标按钮,并调用不同的项目的悬停事件?
更好的解释,你的解释是混乱的,你希望在项目按这样的情况发生,当鼠标悬停的项目 – eyllanesc
所以,我想单击项目旁边的某个位置,同时将光标移动到该位置上(并且仍然单击鼠标按钮),应该触发该项目的悬停事件。 –
我了解你想要的事件,当它发生时你想要做什么任务? – eyllanesc