如何在使用QPolygonF进行绘制时在QGraphicsPolygonItem中添加文本?

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

我想知道您如何在QGraphicsPolygonItem中添加文本?在我的脚本中,我使用QPolygonFsetPolygon绘制项目,我想知道是否可以在其中插入文本?

我正在做一些研究,但我能发现的是,有些人使用QGraphicsTextItem而不是多边形项目或使用了QPainter,而我不知道如何将其与QPolygonF结合使用。有人可以建议我如何解决我的问题吗?

编辑:

很抱歉,没有提供任何示例。这是多边形项目的示例-

from PySide2.QtGui import QColor, QPolygonF, QPen, QBrush
from PySide2.QtCore import Qt, QPointF, QPoint
from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsPolygonItem, QApplication, \
    QFrame, QSizePolicy

points_list = [[60.1, 19.6, 0.0], [60.1, 6.5, 0.0], [60.1, -6.5, 0.0], [60.1, -19.6, 0.0], [60.1, -19.6, 0.0],
               [20.0, -19.6, 0.0], [-20, -19.6, 0.0], [-60.1, -19.6, 0.0], [-60.1, -19.6, 0.0], [-60.1, -6.5, 0.0],
               [-60.1, 6.5, 0.0], [-60.1, 19.6, 0.0], [-60.1, 19.6, 0.0], [-20.0, 19.6, 0.0], [20.0, 19.6, 0.0],
               [60.1, 19.6, 0.0]]


class MainWindow(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent=parent)
        self.create()

    def create(self, **kwargs):
        main_layout = QVBoxLayout()
        graphics = MainGraphicsWidget()
        main_layout.addWidget(graphics)
        self.setLayout(main_layout)


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

        self._scene = QGraphicsScene(backgroundBrush=Qt.gray)
        self.setScene(self._scene)
        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
        self.setFrameShape(QFrame.NoFrame)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

        self.testButton = GraphicsButton()
        self._scene.addItem(self.testButton)


class GraphicsButton(QGraphicsPolygonItem):
    def __init__(self, parent=None):
        super(GraphicsButton, self).__init__(parent)
        self.myPolygon = QPolygonF([QPointF(v1, v2) for v1, v2, v3 in points_list])
        self.setPen(QPen(QColor(0, 0, 0), 0, Qt.SolidLine, Qt.FlatCap, Qt.MiterJoin))
        self.setPolygon(self.myPolygon)
        self.setBrush(QColor(220, 40, 30))


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setGeometry(500, 100, 500, 900)
    window.show()
    sys.exit(app.exec_())

所以这里应该是红色方块,如中间的项目,并且有人知道如何在其中放置一些文本吗?

以下是我想获取的形状和文字的屏幕截图:Shapes and text

python pyqt qgraphicspolygonitem
1个回答
0
投票

最简单的解决方案是添加一个QGraphicsSimpleTextItem,它是图形项的children。对于简单的形状,例如矩形和regular polygons,您可以将项目放置在项目的中心。请记住,文本不会考虑父项的形状,因此您必须以某种方式进行处理。这意味着您必须考虑文本widthheight以及父项的形状(对于不规则形状,三角形和旋转正方形都是正确的)。

class GraphicsButton(QGraphicsPolygonItem):
    def __init__(self, parent=None):
        # ...
        self.textItem = QGraphicsSimpleTextItem('I am a very large rectangle', self)
        rect = self.textItem.boundingRect()
        rect.moveCenter(self.boundingRect().center())
        self.textItem.setPos(rect.topLeft())

如您所见,结果超出父边界:

text outside the parent item

一种可能的替代方法是使用QGraphicsTextItem并设置其textWidth

        self.textItem = QGraphicsTextItem(self)
        self.textItem.setHtml('<center>I am a very large rectangle</center>')
        self.textItem.setTextWidth(self.boundingRect().width())
        rect = self.textItem.boundingRect()
        rect.moveCenter(self.boundingRect().center())
        self.textItem.setPos(rect.topLeft())

text inside the parent item

[请注意,与QGraphicsSimpleTextItem(使用默认的黑色进行绘画)相反,QGraphicsTextItem使用窗口小部件的当前调色板WindowText role,该调色板是从应用程序,视图或从任何视图父级继承的已进行了设置(一旦设置了默认的文本颜色,即使调色板已更改,它也不会更改)。

© www.soinside.com 2019 - 2024. All rights reserved.