确定点击事件中的椭圆

时间:2018-11-19 21:04:39

标签: python pyqt pyqt5 qgraphicsview qgraphicsscene

我是python和PyQT的新手,已经陷入困境。抱歉,如果我的代码有点混乱。我已经花了很多时间进行搜索,但是还没有找到任何可以帮助我解决问题的方法。

我正在尝试确定单击了哪个椭圆。

我基本上有一个部分,可以创建一个椭圆形的圆圈,并根据列表数组中的值在其上打印文本:

class MyFrame(QtWidgets.QGraphicsView):
    def __init__(self, parent = None ):
        super(MyFrame, self).__init__(parent)

        self.setScene(QtWidgets.QGraphicsScene())
        self.setBackgroundBrush(QtGui.QColor(QtCore.Qt.darkGray))
        self.setRenderHints(self.renderHints() | QtGui.QPainter.Antialiasing  | QtGui.QPainter.SmoothPixmapTransform)

        arLights = list( {} for i in xrange(12) )
        circX = 0
        circY = 0
        maxX = 0
        maxY = 0
        i = 0
        pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.lightGray).darker(50))
        brush = QtGui.QBrush(QtGui.QColor(QtCore.Qt.lightGray))
        font = QtGui.QFont('White Rabbit')
        font.setPointSize(12)

        arLights = getLights()

        theta = radians(360)
        columns = len(filter(None, arLights))
        alpha = theta / columns

        for a in range(columns):
            angle = a * alpha
            circX = (w + x) * cos(angle)
            circY = (h + y) * sin(angle)

            item = callbackEllipse(circX, circY, w, h) #(x+xi*(w+x), y+yi*(h+y), w, h)
            item.setAcceptHoverEvents(True)
            item.setPen(pen)
            item.setBrush(brush)
            self.scene().addItem(item)
            item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
            self.writeText(arrLight[i]['name'], circX + (w / 4), circY + (h * 0.75))
            i = i + 1

这是我的mouseReleaseEvent,然后我在其中尝试使用与我以前对其进行布局的相同公式来得出与该椭圆相关的名称。

class callbackEllipse(QtWidgets.QGraphicsEllipseItem):
    def mouseReleaseEvent(self, event):
        # recolor on click
        color = QtGui.QColor(QtCore.Qt.lightGray)
        brush = QtGui.QBrush(color)
        QtWidgets.QGraphicsEllipseItem.setBrush(self, brush)

        theta = radians(360)
        alpha = theta / arrRange

        arrLights = getLights()
        for a in xrange(0, len(filter(None, arrLights))):
            if (event.scenePos().x() > ((w + x) * cos(a * alpha))
                and event.scenePos().x() < (((w + x) * cos(a * alpha)) + w)
                and event.screenPos().y() < ((h + y) * sin(a * alpha))
                and event.screenPos().y() < (((h + y) * sin(a * alpha)) + h)
                ):
                print(arrLights[a]['name'])
                break

        return QtWidgets.QGraphicsEllipseItem.mouseReleaseEvent(self, event)

这不符合预期。我已经尝试过.sceenPos().screenPos()和.pos(),但是它们似乎与我创建椭圆时的坐标不匹配,导致其识别了错误的关联文本或找不到完全匹配。

有人可以帮助我识别椭圆吗,或者为什么我的代码无法按我想要的方式工作?似乎没有一种“命名”它们的方法,这会更容易。

1 个答案:

答案 0 :(得分:1)

不要不必要地工作2次,保存值引用并打印出来,因为mouseReleaseEvent()仅在按下该项目时才会调用:

import math
from PyQt5 import QtCore, QtGui, QtWidgets

def getLights():
    return [{"name": str(i)} for i in range(10)]

class CallbackEllipse(QtWidgets.QGraphicsEllipseItem):
    def __init__(self, light, *args, **kwargs):
        super(CallbackEllipse, self).__init__(*args, **kwargs)
        self._light = light

    def mouseReleaseEvent(self, event):
        color = QtGui.QColor(QtCore.Qt.lightGray)
        brush = QtGui.QBrush(color)
        self.setBrush(brush)    
        print(self._light["name"])
        super(CallbackEllipse, self).mouseReleaseEvent(event)

class MyFrame(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(MyFrame, self).__init__(parent)

        self.setScene(QtWidgets.QGraphicsScene(self))
        self.setBackgroundBrush(QtGui.QColor(QtCore.Qt.darkGray))
        self.setRenderHints(self.renderHints() | QtGui.QPainter.Antialiasing  | QtGui.QPainter.SmoothPixmapTransform)

        arLights = getLights()

        theta = math.radians(360)
        filter_lights = list(filter(None, arLights)) 
        num_of_columns = len(filter_lights)
        delta = theta/num_of_columns
        circX, circY = 0, 0
        w, h, x, y = 100, 100, 100, 100

        pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.lightGray).darker(50))
        brush = QtGui.QBrush(QtGui.QColor(QtCore.Qt.lightGray))

        for i, light in enumerate(filter_lights):
            angle = i*delta
            circX = (w + x) * math.cos(angle)
            circY = (h + y) * math.sin(angle)
            item = CallbackEllipse(light, circX, circY, w, h)
            item.setAcceptHoverEvents(True)
            item.setPen(pen)
            item.setBrush(brush)
            item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
            self.scene().addItem(item)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MyFrame()
    w.show()
    sys.exit(app.exec_())