首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QGraphicsPathItem .setPen()影响文本样式

QGraphicsPathItem .setPen()影响文本样式
EN

Stack Overflow用户
提问于 2020-02-01 14:33:28
回答 1查看 107关注 0票数 1

堆栈溢出社区。让我解释一下我的问题,引用下面的代码片段

代码语言:javascript
复制
from PyQt5 import QtCore, QtGui, QtWidgets


class PortItem(QtWidgets.QGraphicsPathItem):
    def __init__(self, parent=None):
        super().__init__(parent)
        pen=QtGui.QPen(QtGui.QColor("black"), 2)
        self.setPen(pen)        
        self.end_ports = []
        self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable | QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)


class Symbol_Z(PortItem):
    __partCounter=0

    def __init__(self):
        super().__init__()
        self.__partName= "FixedTerms"
        self.__partCounter+=1
        self.drawSymbol()

    def drawSymbol(self):
        path=QtGui.QPainterPath()
        path.moveTo(0, 40)
        path.lineTo(20, 40)
        path.addRect(QtCore.QRectF(20, 30, 40, 20))
        path.moveTo(60, 40)
        path.lineTo(80, 40)
        path.addText(20, 25, QtGui.QFont('Times', 20), self.__partName)
        self.setPath(path)


class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, scene=None, parent=None):
        super().__init__(scene, parent)
        self.setRenderHints(QtGui.QPainter.Antialiasing)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        scene=QtWidgets.QGraphicsScene()
        graphicsview=GraphicsView(scene)

        item=Symbol_Z()
        item.setPos(QtCore.QPointF(0, 250))
        scene.addItem(item)

        self.setCentralWidget(graphicsview)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

我的问题是台词:

  • pen=QtGui.QPen(QtGui.QColor("black"),2)
  • self.setPen(pen)

影响这条线:

  • path.addText(20,25,QtGui.QFont(“时代”,20),self.__partName)

你知道如何添加文本独立吗?当我试图添加它时,我遇到了这样的问题:绘图和文本没有连接,只有符号可以用鼠标鼠标移动,而不能同时使用鼠标(绘图和文本)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-01 17:03:46

一种可能的解决方案是创建另一个QGraphicsPathItem,它是项目的子元素,因此相对坐标不会改变,并且父的QPen不会影响他。

代码语言:javascript
复制
def drawSymbol(self):
    path = QtGui.QPainterPath()
    path.moveTo(0, 40)
    path.lineTo(20, 40)
    path.addRect(QtCore.QRectF(20, 30, 40, 20))
    path.moveTo(60, 40)
    path.lineTo(80, 40)
    self.setPath(path)

    text_item = QtWidgets.QGraphicsPathItem(self)
    text_item.setBrush(QtGui.QColor("black"))
    child_path = QtGui.QPainterPath()
    child_path.addText(20, 25, QtGui.QFont("Times", 20), self.__partName)
    text_item.setPath(child_path)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60017848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档