python - 删除所选项目上的虚线矩形

标签 python python-2.7 pyqt pyqt4 qgraphicsview

我一直在尝试通过查看这里的示例来了解有关 QGraphicsView 小部件的更多信息:

Toggle QPen for selected items after QGraphicsScene selection change

我发现这相当有帮助,但是我注意到,在选择一个项目时,会弹出一个选框。我一直在寻找摆脱这个问题的方法,似乎在 C++ 中对此有一个一致的答案,我在这里看到了一个例子:

https://www.qtcentre.org/threads/39659-About-QGraphicsItem-question

但是我对如何将其转换为 pyqt 感到非常困惑,所以我想知道是否有人可以提供有关如何在第一个链接的示例项目中执行此操作的任何见解......

我一直在通过查看如下代码来寻找可能的等效项:

https://www.riverbankcomputing.com/pipermail/pyqt/2012-November/032110.html

虽然说实话我什至不知道这些示例代码的应用是什么......

最佳答案

如果你想从 python 中的项目选择中删除矩形,你必须使用以下命令:

class GraphicsEllipseItem(QGraphicsEllipseItem):
    def paint(self, painter, option, widget):
        option.state &= ~QStyle.State_Selected
        super(GraphicsEllipseItem, self).paint(painter, option, widget)

完整的脚本

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import random


class GraphicsEllipseItem(QGraphicsEllipseItem):
    def paint(self, painter, option, widget):
        option.state &= ~QStyle.State_Selected
        super(GraphicsEllipseItem, self).paint(painter, option, widget)


class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super(MyGraphicsView, self).__init__()
        self.setDragMode(QGraphicsView.RubberBandDrag)
        self._isPanning = False
        self._mousePressed = False
        self.setCacheMode(QGraphicsView.CacheBackground)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setScene(MyGraphicsScene(self))
        self.scene().selectionChanged.connect(self.selection_changed)
        self._current_selection = []

    def select_items(self, items, on):
        pen = QPen(
            QColor(255, 255, 255) if on else QColor(255, 128, 0),
            0.5,
            Qt.SolidLine,
            Qt.RoundCap,
            Qt.RoundJoin,
        )
        for item in items:
            item.setPen(pen)

    def selection_changed(self):
        try:
            self.select_items(self._current_selection, False)
            self._current_selection = self.scene().selectedItems()
            self.select_items(self._current_selection, True)
        except RuntimeError:
            pass

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self._mousePressed = True
            if self._isPanning:
                self.setCursor(Qt.ClosedHandCursor)
                self._dragPos = event.pos()
                event.accept()
            else:
                super(MyGraphicsView, self).mousePressEvent(event)
        elif event.button() == Qt.MiddleButton:
            self._mousePressed = True
            self._isPanning = True
            self.setCursor(Qt.ClosedHandCursor)
            self._dragPos = event.pos()
            event.accept()

    def mouseMoveEvent(self, event):
        if self._mousePressed and self._isPanning:
            newPos = event.pos()
            diff = newPos - self._dragPos
            self._dragPos = newPos
            self.horizontalScrollBar().setValue(
                self.horizontalScrollBar().value() - diff.x()
            )
            self.verticalScrollBar().setValue(
                self.verticalScrollBar().value() - diff.y()
            )
            event.accept()
        else:
            super(MyGraphicsView, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self._isPanning:
                self.setCursor(Qt.OpenHandCursor)
            else:
                self._isPanning = False
                self.setCursor(Qt.ArrowCursor)
            self._mousePressed = False
        elif event.button() == Qt.MiddleButton:
            self._isPanning = False
            self.setCursor(Qt.ArrowCursor)
            self._mousePressed = False
        super(MyGraphicsView, self).mouseReleaseEvent(event)

    def mouseDoubleClickEvent(self, event):
        self.fitInView(self.sceneRect(), Qt.KeepAspectRatio)
        pass

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space and not self._mousePressed:
            self._isPanning = True
            self.setCursor(Qt.OpenHandCursor)
        else:
            super(MyGraphicsView, self).keyPressEvent(event)

    def keyReleaseEvent(self, event):
        if event.key() == Qt.Key_Space:
            if not self._mousePressed:
                self._isPanning = False
                self.setCursor(Qt.ArrowCursor)
        else:
            super(MyGraphicsView, self).keyPressEvent(event)

    def wheelEvent(self, event):
        # zoom factor
        factor = 1.25

        # Set Anchors
        self.setTransformationAnchor(QGraphicsView.NoAnchor)
        self.setResizeAnchor(QGraphicsView.NoAnchor)

        # Save the scene pos
        oldPos = self.mapToScene(event.pos())

        # Zoom
        if event.delta() < 0:
            factor = 1.0 / factor
        self.scale(factor, factor)

        # Get the new position
        newPos = self.mapToScene(event.pos())

        # Move scene to old position
        delta = newPos - oldPos
        self.translate(delta.x(), delta.y())


class MyGraphicsScene(QGraphicsScene):
    def __init__(self, parent):
        super(MyGraphicsScene, self).__init__(parent)
        self.setBackgroundBrush(QBrush(QColor(50, 50, 50)))
        # self.setSceneRect(50,50,0,0)


class MyMainWindow(QMainWindow):
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.setWindowTitle("Test")
        self.resize(800, 600)
        self.gv = MyGraphicsView()
        self.setCentralWidget(self.gv)
        self.populate()

    def populate(self):
        scene = self.gv.scene()
        for i in range(500):
            x = random.randint(0, 1000)
            y = random.randint(0, 1000)
            r = random.randint(2, 8)
            rect = GraphicsEllipseItem(x, y, r, r)
            rect.setPen(
                QPen(QColor(255, 128, 0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
            )
            rect.setBrush(QBrush(QColor(255, 128, 20, 128)))
            scene.addItem(rect)
            rect.setFlag(QGraphicsItem.ItemIsSelectable)
            rect.setFlag(QGraphicsItem.ItemIsMovable)

        rect = GraphicsEllipseItem(300, 500, 20, 20)
        rect.setPen(
            QPen(QColor(255, 128, 0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
        )
        rect.setBrush(QBrush(QColor(255, 0, 0, 128)))
        scene.addItem(rect)
        rect.setFlag(QGraphicsItem.ItemIsSelectable)
        rect.setFlag(QGraphicsItem.ItemIsMovable)


def main():
    app = QApplication(sys.argv)
    ex = MyMainWindow()
    ex.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

option.state &= ~QStyle.State_Selected 的说明:

state是QStyleOptionGraphicsItem的一个属性,它存储绘画状态的信息,可能包含以下标志的组合:

enter image description here

在这种情况下,可以使用“|”将标志组合起来运算符,并使用“&~”停用。

为了更好地理解,我们用下面的例子:我们将State_Active和State_Editing中的状态设置为初始状态:

state = State_Active | State_Editing
      = 0x00010000 | 0x00400000
      = 0x00410000

并停用 State_Active 标志:

state & ~State_Active
0x00410000 & ~(0x00010000)
0x00410000 &   0xFFFEFFFF
0x00400000

正如您所看到的,仅通过二进制运算,标志 State_Active 被删除,而没有删除其他标志。

那么你想如何停用 State_Selected 标志并将其替换为必须完成的状态:

option.state = option.state & ~QStyle.State_Selected
option.state &= ~QStyle.State_Selected

关于python - 删除所选项目上的虚线矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57505320/

相关文章:

python - 是否可以按降序使用 argsort?

python - 创建一个倒序的优先级队列

python - 使用 python 脚本加载 python 解释器

python - 尝试 dash 应用程序时使用 stat 重新启动

python - 使用 PySide 在 QLabel 中显示视频流

python - 如何在python中将文件保存为excel时显示进度条?

python - 在 Django 中确定和实现趋势算法

python - 比较数据框中的 2 个连续单元格

python - 将 Pytest 测试文件打包为可执行文件

python - GPIO 事件未显示在 QTreeModel/QWidget/QMainWindow 中