如何处理Qt中的鼠标事件? [英] How to handle mouse events in Qt?

查看:196
本文介绍了如何处理Qt中的鼠标事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Qt场景中拖动,平移和鼠标跟踪项目是我要在我的应用程序中同时使用的3个操作。但是,比我想象的要困难得多



示例代码:

  from PySide.QtCore import * 
从PySide.QtGui导入*

类视图(QGraphicsView):
#如何编程鼠标事件
def mouseMoveEvent(self,event):
打印查看MOVE,event.pos()

类场景(QGraphicsScene):
pass

class CircleForTrackingSwitch(QGraphicsEllipseItem)
def __init __(self,scene):
super(CircleForTrackingSwitch,self).__ init __()
self.scene = scene
self.view = self.scene.views() [0]
self.setRect(QRect(20,20,20,20))
self.scene.addItem(self)

def mousePressEvent(self,event):
如果self.view.hasMouseTracking():
self.view.setMouseTracking(False)
else:
self.view.setMouseTracking(True)
printCircle查看SetTrack,event.pos()

def mouseMoveEvent(self ,
printCircle MOVE,event.pos()

class DraggableRectangle(QGraphicsRectItem):
def __init __(self,scene):
super (DraggableRectangle,self).__ init __()
self.scene = scene
self.setRect(QRect(-20,-20,40,40))
self.scene.addItem(self )
#self.setFlag(QGraphicsItem.ItemIsMovable,True)

def mousePressEvent(self,event):
printRectangle PRESS,event.pos()

def mouseMoveEvent(self,event):
printRectangle MOVE,event.pos()
self.setPos(event.pos())

def mouseReleaseEvent(self,event):
打印Rectangle RELEASE,event.pos()

类窗口(QMainWindow):
def __init __(self):
QMainWindow .__ init __(self)
self.s = Scene()
self.s.setSceneRect(-200,-100,300,300,)

self.v = View(self .s)
self.v.setDragMode(QGraphicsView.Sc rollHandDrag)
self.setCentralWidget(self.v)

CircleForTrackingSwitch(self.s)
DraggableRectangle(self.s)

如果__name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.resize(300,200)
window.show ()
sys.exit(app.exec_())

MASTER QUESTION:
如何以编程方式附加鼠标事件?非常感谢。

解决方案

对于可拖动的Rectangle,由于Rectangle已经可以移动,只需要将其子类化,不同的鼠标功能(跟踪的东西),然后调用父的鼠标事件。基本上,你可以使用这个矩形类来获得你想要的东西:

  class DraggableRectangle(QGraphicsRectItem):
def __init __
super().__ init __(* args,** kwargs)
self.scene = scene
self.setRect(QRect(-20 ,-20,40,40))
self.scene.addItem(self)
self.setFlag(QGraphicsItem.ItemIsMovable,True)#保持

def mousePressEvent(self ,event):
打印Rectangle PRESS,event.pos()
super()。mousePressEvent(event)#调用parent

def mouseMoveEvent(self,event)
打印矩形MOVE,event.pos()
#不要自动移动
#调用已处理移动的父项
super()。mouseMoveEvent(event)

def mouseReleaseEvent(self,event):
打印Rectangle RELEASE,event.pos()
super()。mouseReleaseEvent(event)#调用parent

我希望这是你要找的。

Dragging, panning and mouse tracking items in Qt scene are the 3 actions which I want to simultaneously use in my app. However, it is more difficult than I thought.

Example code :

from PySide.QtCore import *
from PySide.QtGui import *

class View(QGraphicsView):
    """# HOW TO ATTACH MOUSE EVENTS PROGRAMMATICALLY ?
    def mouseMoveEvent(self, event):
        print "View MOVE", event.pos()
    """
class Scene(QGraphicsScene):
    pass

class CircleForTrackingSwitch(QGraphicsEllipseItem):
    def __init__(self, scene):
        super(CircleForTrackingSwitch, self).__init__()
        self.scene = scene
        self.view = self.scene.views()[0]
        self.setRect(QRect(20,20,20,20))
        self.scene.addItem(self)

    def mousePressEvent(self, event):
        if self.view.hasMouseTracking() :
            self.view.setMouseTracking(False)
        else :
            self.view.setMouseTracking(True)
        print "Circle View SetTrack", event.pos()

    def mouseMoveEvent(self, event):
        print "Circle MOVE", event.pos()

class DraggableRectangle(QGraphicsRectItem):
    def __init__(self, scene):
        super(DraggableRectangle, self).__init__()
        self.scene = scene
        self.setRect(QRect(-20,-20,40,40))
        self.scene.addItem(self)
        #self.setFlag(QGraphicsItem.ItemIsMovable, True)

    def mousePressEvent(self, event):
        print "Rectangle PRESS", event.pos()

    def mouseMoveEvent(self, event):
        print "Rectangle MOVE", event.pos()
        self.setPos(event.pos())

    def mouseReleaseEvent(self, event):
        print "Rectangle RELEASE", event.pos()

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.s = Scene()
        self.s.setSceneRect(-200,-100,300,300,)

        self.v = View(self.s)
        self.v.setDragMode(QGraphicsView.ScrollHandDrag)
        self.setCentralWidget(self.v)

        CircleForTrackingSwitch(self.s)
        DraggableRectangle(self.s)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.resize(300, 200)
    window.show()
    sys.exit(app.exec_())

MASTER QUESTION : How to attach mouse events programmatically? Many thanks.

解决方案

For the draggable Rectangle, since the Rectangle is already moveable, you only have to subclass it, to overload the different mouse functions (with the track stuff), then to call the parent's mouse event. Basically, you could have what you want with this rectangle class:

class DraggableRectangle(QGraphicsRectItem):
    def __init__(self, scene, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.scene = scene
        self.setRect(QRect(-20,-20,40,40))
        self.scene.addItem(self)
        self.setFlag(QGraphicsItem.ItemIsMovable, True) # Keep that

    def mousePressEvent(self, event):
        print "Rectangle PRESS", event.pos()
        super().mousePressEvent(event) # Call parent

    def mouseMoveEvent(self, event):
        print "Rectangle MOVE", event.pos()
        # Do not move by yourself
        # Call parent that already handles the move
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        print "Rectangle RELEASE", event.pos()
        super().mouseReleaseEvent(event) # Call parent

I hope this is what you were looking for.

这篇关于如何处理Qt中的鼠标事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆