在QGraphicsScene中的GGraphicsEllipseItem中心对齐QLabel。

问题描述 投票:0回答:1

我想知道是否有办法将Qlabel定位在我用PyQt设计的饼图中的 "扇形 "中间。扇形图是QgraphicsEllipseItems,我为其分配了相应的起始角和跨度角。到目前为止,我已经尝试将标签放在QGraphicsProxyItem中,并将 "Slice "或 "Sector "设置为父节点。然后,我尝试将标签定位在 "扇形 "的中间。然而,问题是boundingRect是一个矩形,而不仅仅是 "扇形 "本身。

当前的饼图

在上图中,你可以看到问题所在。标签的位置不正确。我试图用标签实现这样的效果。

带有正确位置标签的模型图

当前的代码。

    pie = QtWidgets.QGraphicsEllipseItem(60, 110, self.piechart.width, self.piechart.height)
    pie.setTransformOriginPoint(pie.boundingRect().center())
    pie.setZValue(-1)
    self.scene.addItem(pie)

    start_angle = 0


    for sector in self.piechart.sectors:
        slice = QtWidgets.QGraphicsEllipseItem(60, 110, self.piechart.width, self.piechart.height, parent=pie)
        color = QtGui.QBrush(sector.color)
        slice.setBrush(color)
        span_angle = sector.proportional_volume*360*16
        slice.setStartAngle(start_angle)
        slice.setSpanAngle(span_angle)
        start_angle += span_angle

        label = QtWidgets.QLabel(str(round(sector.proportional_volume*100,2))+'%', alignment = QtCore.Qt.AlignCenter)
        label.setAutoFillBackground(False)
        label.setStyleSheet('background-color: transparent')
        font = QtGui.QFont('Times', 8)
        font.setBold(True)
        label.setFont(font)

        proxy = QtWidgets.QGraphicsProxyWidget(slice)
        proxy.setWidget(label)
        proxy.setPos(slice.boundingRect().center()) 

        self.scene.addItem(slice)
        self.scene.addItem(proxy)
python pyqt pyqt5 qgraphicsitem
1个回答
1
投票

由于几何学的原因,你不能使用边界矩形的中心(从基本的角度来看)。QGraphicsItem.boundingRect() 定义)。)

这个纯虚拟函数将项目的外部边界定义为一个矩形。

这意味着,无论项目的形状是什么,边界矩形都是一个包括 项目的形状。在下图中,你可以清楚地看到,"这不是你要找的中心。" [引者注]。

bounding rect center not in the middle

你实际上需要的是中心在 中轴 的每一个片断,所以只需将该片断范围的一半除以半径的一半即可。

请注意,图形项(就像widgets一样)通常使用它们的左上角作为它们的位置参考,如果你使用 "中间 "作为参考,你将得不到正确的结果。

label not centered

为了避免这种情况,你必须减去标签矩形的中心,这样它就会在你想要的地方正确居中。

        # create a common rectangle, since we're going to use the same coordinates
        # for all ellipses
        rect = QtCore.QRectF(60, 110, self.piechart.width, self.piechart.height)
        pie = QtWidgets.QGraphicsEllipseItem(rect)
        pie.setTransformOriginPoint(pie.boundingRect().center())
        pie.setZValue(-1)
        self.scene.addItem(pie)

        # the center position is half of the radius, or a quarter of the size
        labelRadius = self.piechart.width * .25
        center = rect.center()
        start_angle = 0


        for sector in self.piechart.sectors:
            slice = QtWidgets.QGraphicsEllipseItem(rect, parent=pie)
            slice.setPen(QtGui.QPen(QtCore.Qt.NoPen))
            color = QtGui.QBrush(sector.color)
            slice.setBrush(color)
            span_angle = sector.proportional_volume*360*16
            slice.setStartAngle(start_angle)
            slice.setSpanAngle(span_angle)
            # we need the current start_angle later, let's do this later
            # start_angle += span_angle

            label = QtWidgets.QLabel(str(round(sector.proportional_volume*100,2))+'%', alignment = QtCore.Qt.AlignCenter)
            label.setAutoFillBackground(False)
            label.setStyleSheet('background-color: transparent')
            font = QtGui.QFont('Times', 8)
            font.setBold(True)
            label.setFont(font)

            proxy = QtWidgets.QGraphicsProxyWidget(slice)
            proxy.setWidget(label)

            # get the middle angle of the slice
            angle = start_angle / 16 + sector.proportional_volume * 180

            # create a line that has a length that's a quarter of the radius, and
            # has an angle that is in the middle of the current slice; finally,
            # get its end point (p2)
            line = QtCore.QLineF.fromPolar(labelRadius, angle)
            middlePoint = line.p2()

            # the above can also be achieved by using trigonometry functions; note
            # that the sine is negative, since QGraphicsScene coordinates are
            # inverted vertically (downwards coordinates are positive)
            # x = cos(radians(angle)) * labelRadius
            # y = -sin(radians(angle)) * labelRadius
            # middlePoint = QtCore.QPointF(x, y)

            # finally, translate the point to the center of the ellipse
            middlePoint += center
            r = QtCore.QRectF(-1, -1, 2, 2)
            self.scene.addEllipse(r.translated(middlePoint))

            # center the label at that point by subtracting its own center
            proxy.setPos(middlePoint - label.rect().center())

            start_angle += span_angle

            # when you create a QGraphicsItem with a parent that has already been
            # added to the scene, there's no need to add it again
            # self.scene.addItem(slice)
            # self.scene.addItem(proxy)

考虑到从图形的角度来看,实际的 "中间 "位置往往会被认为更接近中心,这是因为基于切片的几何形状和字符的视觉感知而产生的光学错觉;下图是基于上面的代码,你可以看到右边的浅绿色小切片(4.76%)似乎偏移了,即使是在 正确地居中。

final result

所以,你可能会喜欢使用一个稍微高一点的比率,用于... labelRadius (例如,0.3)。

最后,两个考虑因素。

  • 一些百分比可能非常小,你可能会最终导致一些标签被下一个片子部分隐藏;你可能应该避免将标签作为片子的父级。
  • 在简单的QLabel中使用QGraphicsProxyWidget是不必要的,可以考虑使用 QGraphicsSimpleTextItemQGraphicsTextItem但请仔细阅读他们的文档,因为他们的坐标系并不完全相同。
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.