내 응용 프로그램에는 사용자가 마우스 버튼을 클릭하여 항목 위로 마우스를 가져 가서 항목의 색상을 변경할 수 있어야하는 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