本文介绍了QRubberBand.geometry().intersects(???) 如何在 QGraphicsScene 中找到相交的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很少有代码可以演示intersects,但主要是按钮.像这样:

I found few codes which demonstrates intersects, but it was mainly buttons. Something like this:

for child in self.findChildren(QPushButton):
    if rect.intersects(child.geometry( )):
        selected.append(child)

但是您如何找到在您的 GraphicsScene 中与Marquee"选择(QRubberBand)相交的图像?我尝试用 QPixmap、QGraphicsScene、QGraphicsPixmapItem 替换 findChildren(QPushButton) 它总是给我返回空列表.

But how do you find images intersecting in your GraphicsScene with "Marquee" selection(QRubberBand)? I tried replacing findChildren(QPushButton) with QPixmap, QGraphicsScene, QGraphicsPixmapItem it always giving me back empty list.

推荐答案

如果您使用具有 QGraphicsView 的 QRubberBand,则必须使用rubberBandChanged 信号,并且在 items 方法旁边,您将获得 QRubberBand 下方的项目.

If you are using the QRubberBand that has the QGraphicsView you have to use the rubberBandChanged signal and next to the items method you get the items that are below the QRubberBand.

from PyQt5 import QtCore, QtGui, QtWidgets
import random


def create_pixmap():
    pixmap = QtGui.QPixmap(100, 100)
    pixmap.fill(QtGui.QColor(*random.sample(range(255), 3)))
    return pixmap


class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.setScene(QtWidgets.QGraphicsScene(self))
        self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
        self.rubberBandChanged.connect(self.on_rubberBandChanged)

        for _ in range(5):
            item = QtWidgets.QGraphicsPixmapItem(create_pixmap())
            item.setPos(*random.sample(range(500), 2))
            self.scene().addItem(item)

    @QtCore.pyqtSlot("QRect", "QPointF", "QPointF")
    def on_rubberBandChanged(
        self, rubberBandRect, fromScenePoint, toScenePoint
    ):
        r = QtCore.QRectF(fromScenePoint, toScenePoint)
        selected = self.items(rubberBandRect)
        print(selected)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = GraphicsView()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

如果您使用另一个 QRubberBand,则逻辑与此类似,因为您必须使用 items() QGraphicsView 方法

If you are using another QRubberBand the logic is similar to that since you must use the items() method of QGraphicsView

from PyQt5 import QtCore, QtGui, QtWidgets
import random


def create_pixmap():
    pixmap = QtGui.QPixmap(100, 100)
    pixmap.fill(QtGui.QColor(*random.sample(range(255), 3)))
    return pixmap


class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.setScene(QtWidgets.QGraphicsScene(self))
        self._rubberBand = QtWidgets.QRubberBand(
            QtWidgets.QRubberBand.Rectangle, self.viewport()
        )
        self._rubberBand.hide()
        self._origin = QtCore.QPoint()

        for _ in range(5):
            item = QtWidgets.QGraphicsPixmapItem(create_pixmap())
            item.setPos(*random.sample(range(500), 2))
            self.scene().addItem(item)

    def mousePressEvent(self, event):
        self._origin = event.pos()
        self._rubberBand.setGeometry(QtCore.QRect(self._origin, QtCore.QSize()))
        self._rubberBand.show()
        super(GraphicsView, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        self._rubberBand.setGeometry(
            QtCore.QRect(self._origin, event.pos()).normalized()
        )

    def mouseReleaseEvent(self, event):
        self._rubberBand.setGeometry(
            QtCore.QRect(self._origin, event.pos()).normalized()
        )
        selected = self.items(self._rubberBand.geometry())
        print(selected)
        self._rubberBand.hide()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = GraphicsView()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

这篇关于QRubberBand.geometry().intersects(???) 如何在 QGraphicsScene 中找到相交的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 19:40