python - 使用 QPolygonF 绘图时如何在 QGraphicsPolygonItem 内添加文本?

标签 python pyqt

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

我正在做一些研究,但我发现的是有些人使用 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_())

所以这里应该是红色方 block 状的项目在中心,有人知道如何在里面放一些文本吗?

这是我想要获得的形状和文本的屏幕截图: Shapes and text

最佳答案

最简单的解决方案是添加 QGraphicsSimpleTextItem这是图形项目的
对于矩形等简单形状和 regular polygons然后您可以将该项目放置在该项目的中心。请记住,文本不会考虑父项的形状,您必须以某种方式处理它;这意味着您必须考虑文本宽度高度以及父项形状(对于不规则形状是这样,对于三角形和旋转正方形也是如此)。

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小部件的属性,继承自应用程序、 View 或任何之前设置过它的父 View (一旦设置了默认文本颜色,即使设置了默认文本颜色,它也不会更改)调色板已更改)。

关于python - 使用 QPolygonF 绘图时如何在 QGraphicsPolygonItem 内添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174317/

相关文章:

python - 将数据从字典加载到 csv

python - 查找字符串中出现的所有字符

python - 大量按钮和智能检查被检查

python - 如何使用 model() 删除项目后清理困惑

python - Python 中使用 asyncio 的多个 websocket 流

python - 有什么方法可以将 **kwargs 添加到 __str__() 的对象签名中?

python - Python 中的图像配准和仿射变换

python - QComboBox 在可编辑和使用模型时不发出信号

python - 在 PyQt5 中从主窗口打开文件到新窗口(在不同的文件中)

python - 在 QTableWidget 中获取选定行数 - PyQt