我正在 PyQt4 中构建,但不知道如何将文本添加到 QGraphicsPolygonItem。这个想法是在用户双击后将文本设置在矩形框的中间(并通过 QInputDialog.getText 获取对话框)。
课程是:
class DiagramItem(QtGui.QGraphicsPolygonItem):
def __init__(self, diagramType, contextMenu, parent=None, scene=None):
super(DiagramItem, self).__init__(parent, scene)
path = QtGui.QPainterPath()
rect = self.outlineRect()
path.addRoundRect(rect, self.roundness(rect.width()), self.roundness(rect.height()))
self.myPolygon = path.toFillPolygon()
我的双击鼠标事件看起来像这样,但没有更新!
def mouseDoubleClickEvent(self, event):
text, ok = QtGui.QInputDialog.getText(QtGui.QInputDialog(),'Create Region Title','Enter Region Name: ', \
QtGui.QLineEdit.Normal, 'region name')
if ok:
self.myText = str(text)
pic = QtGui.QPicture()
qp = QtGui.QPainter(pic)
qp.setFont(QtGui.QFont('Arial', 40))
qp.drawText(10,10,200,200, QtCore.Qt.AlignCenter, self.myText)
qp.end()