堆栈溢出社区。让我解释一下我的问题,引用下面的代码片段
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_())我的问题是台词:
影响这条线:
你知道如何添加文本独立吗?当我试图添加它时,我遇到了这样的问题:绘图和文本没有连接,只有符号可以用鼠标鼠标移动,而不能同时使用鼠标(绘图和文本)。
发布于 2020-02-01 17:03:46
一种可能的解决方案是创建另一个QGraphicsPathItem,它是项目的子元素,因此相对坐标不会改变,并且父的QPen不会影响他。
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)https://stackoverflow.com/questions/60017848
复制相似问题