我如何将鼠标限制在Pyside中的QGraphicsScene中



或qgraphicsview?我仍然不了解映射。请参阅评论

# I'm not computing x and y correctly. mapTo/From/Global/Scene/???

in myview.py 下面。想法是,当您单击戒指时,它会切换移动性,将其绑在鼠标运动中,直到您再次单击为止 - 即,第一个单击将其设置为" drag",然后将其设置为" drop"。

效果很好,但是当我离开"框架"(视图/场景)然后在其他地方重新输入时,戒指会跳到新位置。我想防止在戒指为"移动"或"在"时离开时离开拖动"模式。

主代码

# Form implementation generated from reading ui file 'frames.ui'
# ...and then adjusted by hand... a lot
from PySide.QtCore import *
from PySide.QtGui  import *
from myview        import *

class Frames(QDialog):
    def __init__(self, parent=None):
        super(Frames, self).__init__(parent)
        self.verticalLayout = QVBoxLayout()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.topBox = QGroupBox()
        sizePolicy = QSizePolicy(QSizePolicy.Preferred,
                                 QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.topBox
                                     .sizePolicy().hasHeightForWidth())
        self.topBox.setSizePolicy(sizePolicy)
        self.verticalLayout.addWidget(self.topBox)
        self.middleBox = QGroupBox()
        sizePolicy = QSizePolicy(QSizePolicy.Preferred,
                                 QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(10)
        sizePolicy.setHeightForWidth(self.middleBox
                                     .sizePolicy().hasHeightForWidth())
        self.middleBox.setSizePolicy(sizePolicy)
        self.horizontalLayout = QHBoxLayout(self.middleBox)
        self.view = MyView(self)
        self.horizontalLayout.addWidget(self.view)
        self.rightBox = QGroupBox(self.middleBox)
        sizePolicy = QSizePolicy(QSizePolicy.Preferred,
                                 QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.rightBox
                                     .sizePolicy().hasHeightForWidth())
        self.rightBox.setSizePolicy(sizePolicy)
        self.horizontalLayout.addWidget(self.rightBox)
        self.verticalLayout.addWidget(self.middleBox)
        self.bottomBox = QGroupBox()
        sizePolicy = QSizePolicy(QSizePolicy.Preferred,
                                 QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.bottomBox
                                     .sizePolicy().hasHeightForWidth())
        self.bottomBox.setSizePolicy(sizePolicy)
        self.verticalLayout.addWidget(self.bottomBox)
        self.setLayout(self.verticalLayout)
        self.showFullScreen()
        QMetaObject.connectSlotsByName(self)

def main():
    import sys
    app = QApplication(sys.argv)
    frames = Frames()
    frames.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

myview.py

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

class Ring(QGraphicsEllipseItem):
    def __init__(self, parent=None):
        super(Ring, self).__init__(0, 0, 80, 80, parent)
        self.mobile   = 0     # Initially immobile
        self.setFlags(QGraphicsItem.ItemIsMovable)
        self.setAcceptsHoverEvents(True)
    def mousePressEvent(self, event):
        super(Ring, self).mousePressEvent(event)
        self.mobile = (self.mobile + 1) % 2  # Toggle mobility

class MyView(QGraphicsView):
    def __init__(self, parent=None):
        super(MyView, self).__init__(parent)
        self.installEventFilter(self)
        sizePolicy = QSizePolicy(QSizePolicy.Expanding,
                                 QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(2)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
        self.setSizePolicy(sizePolicy)
        self.scene = QGraphicsScene(self)
        self.ring  = Ring()
        self.scene.addItem(self.ring)
        self.curse = self.cursor()
    def eventFilter(self, obj, event):
        super(MyView, self).eventFilter(obj, event)
        if event.type() == QEvent.WindowActivate:
            self.setWindowFlags(Qt.FramelessWindowHint)
            self.setFrameShape(QFrame.NoFrame)
            bounds = self.geometry()
            self.X1, self.Y1, self.w,  self.h  = bounds.getRect()
            self.X1, self.Y1, self.X2, self.Y2 = bounds.getCoords()
            self.scene.setSceneRect(self.X1, self.Y1,
                                    self.w, self.h)
            self.cx  = bounds.center().x()
            self.cy  = bounds.center().y()
            self.ring.setPos(self.cx - 40, self.cy - 40)
            self.setScene(self.scene)
            brush = QBrush(QColor(255, 255, 127))
            brush.setStyle(Qt.SolidPattern)
            self.setBackgroundBrush(brush)
            self.removeEventFilter(obj)
        return False
    def mouseMoveEvent(self, event):
        super(MyView, self).mouseMoveEvent(event)
        if self.ring.mobile:
# I'm not computing x and y correctly. mapTo/From/Global/Scene/???
            x = min(max(event.pos().x(), self.X1), self.X2)
            y = min(max(event.pos().y(), self.Y1), self.Y2)
#           self.curse.setPos(x, y)
            self.ring.setPos(event.pos())

真正的鼠标光标是由窗口环境管理的,而不是QT,因此我认为没有办法在该级别上执行此操作。至少不是以跨平台的方式。但是您可能可以伪造它。

  • 隐藏真正的光标,也许将其设置为Qt::BlankCursor
  • 跟踪相对于要限制的区域的真实光标的位置。
  • 将自己的虚拟光标绘制为qt中的图形对象。
  • 自己管理虚拟光标的位置。

但是,这不会阻止真正的光标从应用程序中"单击"并与其他应用程序进行交互。您可能必须检测到这种情况,并将光标单击回到您的应用程序中,并恢复对虚拟光标的控制。这样,当真正的光标"回来"时,您可以从上一个位置恢复虚拟光标的移动,而不是真实的光标当前位置。

相关内容

  • 没有找到相关文章

最新更新