【问题标题】:QGraphicsView: how to make rubber band selection appear only on left mouse button?QGraphicsView:如何使橡皮筋选择只出现在鼠标左键上?
【发布时间】:2017-07-30 07:02:01
【问题描述】:

我想制作一个QGraphicsScene 并在QGraphicsView 中显示它。我想通过鼠标中键滚动场景并通过左键选择橡皮筋。但我不知道如何使橡皮筋选择只通过鼠标左键出现。

这是我的代码:

# -*- coding: utf-8 -*-

import os, sys

from PyQt5 import QtWidgets, QtCore, QtGui, QtSvg

class MegaSceneView(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(MegaSceneView, self).__init__(parent)
        self._scale_factor = 1.0

        self._scale_by = 1.2
        self.setAcceptDrops(True)

        self.setRenderHint(QtGui.QPainter.Antialiasing)
        self.setMouseTracking(True)
        self.setRubberBandSelectionMode(QtCore.Qt.IntersectsItemShape)
        self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)

        self._prev_mouse_scene_pos = None


    def mousePressEvent(self, event):
        if (event.buttons() & QtCore.Qt.MidButton) != QtCore.Qt.NoButton:
            self._prev_mouse_scene_pos = (event.pos())
        super(MegaSceneView, self).mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        super(MegaSceneView, self).mouseReleaseEvent(event)
        self._prev_mouse_scene_pos = None

    def mouseMoveEvent(self, event):
        super(MegaSceneView, self).mouseMoveEvent(event)

        if (event.buttons() & QtCore.Qt.MidButton) != QtCore.Qt.NoButton:
            cur_mouse_pos = (event.pos())
            if self._prev_mouse_scene_pos is not None:
                delta_x = cur_mouse_pos.x() - self._prev_mouse_scene_pos.x()
                delta_y = cur_mouse_pos.y() - self._prev_mouse_scene_pos.y()

                self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - delta_x)
                self.verticalScrollBar().setValue(self.verticalScrollBar().value() - delta_y)

            self._prev_mouse_scene_pos = (event.pos())

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)

    mega_view = MegaSceneView()

    mega_scene = QtWidgets.QGraphicsScene(-500, -500, 1000, 1000)
    # mega_scene = QtWidgets.QGraphicsScene()

    rect_item_1 = QtWidgets.QGraphicsRectItem(-30, -20, 60, 40)
    mega_scene.addItem(rect_item_1)

    rect_item_2 = QtWidgets.QGraphicsRectItem(-20, -30, 40, 60)
    mega_scene.addItem(rect_item_2)
    rect_item_2.setPos(300, 200)

    mega_view.setScene(mega_scene)
    mega_view.show()

    sys.exit(app.exec_())

我应该添加什么使橡皮筋只能通过左键显示?

【问题讨论】:

    标签: qt pyqt qt5 qgraphicsview qgraphicsscene


    【解决方案1】:

    没有内置的方法可以做到这一点。您需要为图形视图继承 mousePressEventmouseMoveEventmouseReleaseEvent,并自己创建可见的橡皮筋。 (QRubberBand 可以很好地解决这个问题。)当用户释放鼠标时,您需要将橡皮筋范围转换为场景坐标并调用QGraphicsScene::setSelectionArea

    【讨论】:

      【解决方案2】:

      您可以在视图类的mousePressEventmouseReleaseEvent 函数中设置拖动模式,使其默认保持在RubberBandDrag,但在按住鼠标中键时切换到NoDrag 模式。

      像这样 - c++,但所有语言的想法都是一样的 - :

      void YourViewClass::mousePressEvent(QMouseEvent* event)    {
          if (event->buttons() == Qt::MiddleButton)
              setDragMode(QGraphicsView::NoDrag);
      }
      
      void YourViewClass::mouseReleaseEvent(QMouseEvent* event)    {
          if (event->button() == Qt::MiddleButton)
              setDragMode(QGraphicsView::RubberBandDrag);
      }
      

      【讨论】:

        猜你喜欢
        • 2019-09-05
        • 2015-06-01
        • 2017-05-22
        • 2019-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-21
        • 1970-01-01
        相关资源
        最近更新 更多