【问题标题】:Drawing Triangles with PyQt5使用 PyQt5 绘制三角形
【发布时间】:2015-10-13 02:22:32
【问题描述】:

我有一个QGraphicsScene,我正在尝试通过QGraphicsPolygonItem 添加一个三角形。

class MainScene(QtWidgets.QGraphicsScene):
def __init__(self, *args, parent=None):
    self.brush = QtGui.QBrush('94c3e4')
    QtWidgets.QGraphicsScene.__init__(self, parent, *args)
    self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor.fromRgb(209, 233, 249)))
....
## run every time a point is selected to become part of a triangle
    TrianglePts.append(Points2[indexW].scenePos())
    Points2[indexW].greenSelect()
    if len(TrianglePts) == 3:
        print(TrianglePts)
        triangle = TriangleItem(QtGui.QPolygonF(TrianglePts))
        self.addItem(triangle)


class TriangleItem(QtWidgets.QGraphicsPolygonItem):
    def __init__(self, polygon, parent=None):
        QtWidgets.QGraphicsPolygonItem.__init__(self, polygon, parent)
        self.setBrush(QtGui.QBrush(QtGui.QColor.fromRgb(249, 245, 209)))

由于某种原因,三角形永远不会被渲染。我做了类似于渲染点的事情,它似乎工作。这是自定义类:

class VertexItem(QtWidgets.QGraphicsEllipseItem):
def __init__(self, x, y, parent=None):
    self.x = x
    self.y = y
    QtWidgets.QGraphicsEllipseItem.__init__(self,parent)
    self.setRect(x, y, 20, 20)
    #self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable)
    #self.setFlags(QtWidgets.QGraphicsItem.ItemIsSelectable)
    #self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable)
    self.setAcceptHoverEvents(True)

有人知道我需要做什么来渲染三角形吗?我很乐意提供更多必要的代码。

【问题讨论】:

    标签: python python-3.x pyqt5


    【解决方案1】:

    所以我找到了答案。结果我发送到我的 TriangleItem 的点是空的 - 为了修复它,我替换了

    TrianglePts.append(Points2[indexW].scenePos())
    

    TrianglePts.append(QtCore.QPointF(Points2[indexW].x + 10, Points2[indexW].y + 10))
    

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      相关资源
      最近更新 更多