如何在PySide中将鼠标限制为QGraphicsScene?

时间:2017-02-23 23:33:46

标签: python pyside qgraphicsview coordinate-systems qgraphicsscene

或者QGraphicsView?我还没有理解绘图。见评论

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

在下面的 myview.py 中。这个想法是,当你单击环时,它会切换移动性,将它绑定到鼠标移动,直到你再次点击 - 即第一次点击将其设置为"拖动"第二个是" drop"。

这很好,但是当我离开框架时#34; (视图/场景)然后重新进入其他地方,环跳转到新位置。我希望防止离开,而戒指是"移动"或者在"拖动"模式。

主要代码

# 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())

1 个答案:

答案 0 :(得分:0)

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

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

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