我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用PyQt5.QtWidgets.QGraphicsTextItem()。
def __init__(self, port, text, hOffset, color, highlightColor): super(PortLabel, self).__init__(port) self.__port = port self.__text = text self.__textItem = QtWidgets.QGraphicsTextItem(text, self) self._labelColor = color self.__highlightColor = highlightColor self.__textItem.setDefaultTextColor(self._labelColor) self.__textItem.setFont(self.__font) self.__textItem.transform().translate(0, self.__font.pointSizeF() * -0.5) option = self.__textItem.document().defaultTextOption() option.setWrapMode(QtGui.QTextOption.NoWrap) self.__textItem.document().setDefaultTextOption(option) self.__textItem.adjustSize() self.setPreferredSize(self.textSize()) self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)) self.setWindowFrameMargins(0, 0, 0, 0) self.setHOffset(hOffset) self.setAcceptHoverEvents(True) self.__mousDownPos = None
def createImage(self, transform): if self.type == DemoTextItem.DYNAMIC_TEXT: return None sx = min(transform.m11(), transform.m22()) sy = max(transform.m22(), sx) textItem = QGraphicsTextItem() textItem.setHtml(self.text) textItem.setTextWidth(self.textWidth) textItem.setFont(self.font) textItem.setDefaultTextColor(self.textColor) textItem.document().setDocumentMargin(2) w = textItem.boundingRect().width() h = textItem.boundingRect().height() image = QImage(int(w * sx), int(h * sy), QImage.Format_ARGB32_Premultiplied) image.fill(QColor(0, 0, 0, 0).rgba()) painter = QPainter(image) painter.scale(sx, sy) style = QStyleOptionGraphicsItem() textItem.paint(painter, style, None) return image
def __init__(self, parentTempoTrack, staticExportItem): super().__init__("") #x,y,w,h self.staticExportItem = staticExportItem self.parentTempoTrack = parentTempoTrack if not self.staticExportItem["positionInBlock"] == 0: self.setAcceptHoverEvents(True) self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable) self.setCursor(QtCore.Qt.SizeHorCursor) #this sets the cursor while the mouse is over the item. It is independent of AcceptHoverEvents else: self.ungrabMouse = api.nothing #to surpress a warning from the context menu self.setFont(constantsAndConfigs.musicFont) self.setHtml("<font color='black' size='6'>{}</font>".format(constantsAndConfigs.realNoteDisplay[staticExportItem["referenceTicks"]])) self.number = QtWidgets.QGraphicsTextItem("") self.number.setParentItem(self) self.number.setHtml("<font color='black' size='2'>{}</font>".format(str(int(staticExportItem["unitsPerMinute"])))) self.number.setPos(0,0) if not self.staticExportItem["graphType"] == "standalone": self.arrow = QtWidgets.QGraphicsSimpleTextItem("?") #unicode long arrow right #http://xahlee.info/comp/unicode_arrows.html self.arrow.setParentItem(self) self.arrow.setPos(13,30)
def _add_circles(self, n, add_text_labels=True): """ Adds n circles to the graphic scene. :param n: the number of circles """ pen = QPen(self.circle_line_color) pen.setStyle(Qt.DotLine) pen.setWidth(self.line_width) width, height = self.width(), self.height() stepw, steph = width/n, height/n mx, my = self._get_middle() for i in range(1, n+1): w, h = width - i * stepw, height - i * steph self.scene.addEllipse((width-w)/2, (height-h)/2, w, h, pen) if add_text_labels: text = QGraphicsTextItem() text.setDefaultTextColor(self.text_label_color) text.setPlainText(str(int(w/2))) text.setPos(mx+w/2.0, my) text2 = QGraphicsTextItem() text2.setDefaultTextColor(self.text_label_color) text2.setPlainText(str(int(-w / 2))) text2.setPos(mx - w / 2.0, my) self.scene.addItem(text) self.scene.addItem(text2)
def __init__(self, text, parent=None): super(NodeTitle, self).__init__(parent) self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)) self.__textItem = QtWidgets.QGraphicsTextItem(text, self) self.__textItem.setDefaultTextColor(self.__color) self.__textItem.setFont(self.__font) self.__textItem.setPos(0, -2) option = self.__textItem.document().defaultTextOption() option.setWrapMode(QtGui.QTextOption.NoWrap) self.__textItem.document().setDefaultTextOption(option) self.__textItem.adjustSize() self.setPreferredSize(self.textSize())
def addGraphicsItems(self): self.mainRect = QGraphicsRectItem(QRectF(-15, -15, 30, 30), self) self.nodeTitle = QGraphicsTextItem(type(self).name, self) titleFont = QFont() titleFont.setBold(True) self.nodeTitle.setFont(titleFont) self.selectedChanged(self.isSelected())
def switch_to_text(self, text): font = QFont() font.setPointSize(self.__height_to_point(self.PIX.height())) graphics_text_item = QGraphicsTextItem(text) graphics_text_item.setFlag(QGraphicsItem.ItemIsMovable) graphics_text_item.setFont(font) self.scene().addItem(graphics_text_item) graphics_text_item.setPos( QPoint(self.item.boundingRect().x(), self.item.boundingRect().y())) self.path = QPainterPath() self.item = GraphicsPathItem() self.scene().addItem(self.item)
def __init__(self, ID = 0, x = 0, y = 0): super(self.__class__, self).__init__(ID, x, y) self._kind_name = 'pickingStation' self._items = [] self._graphics_item = QGraphicsRectItem(self) self._items.append(QGraphicsRectItem(self._graphics_item)) self._items.append(QGraphicsRectItem(self._graphics_item)) self._text = QGraphicsTextItem(self._graphics_item)
def __init__(self, ID = 0, x = 0, y = 0): super(self.__class__, self).__init__(ID, x, y) self._kind_name = 'shelf' self._carried = None self._products = [] self._graphics_item = QGraphicsEllipseItem(self) self._graphics_carried = QGraphicsEllipseItem() self._text = QGraphicsTextItem(self) self.setZValue(2.0) self.update_tooltip()
def __init__(self, ID = 0, x = 0, y = 0): super(self.__class__, self).__init__(ID, x, y) self._kind_name = 'robot' self._carries = None self._initial_carries = None self._tasks = [] self._graphics_item = QGraphicsRectItem(self) self._text = QGraphicsTextItem(self) self.setZValue(1.0)
def __init__(self, ID = 0, x = 0, y = 0): super(self.__class__, self).__init__(ID, x, y) self._kind_name = 'checkpoint' self._ids = {} self._graphics_item = QGraphicsRectItem(self) self._text = QGraphicsTextItem(self._graphics_item) self._shine = False
def __init__(self, parentTempoTrack, staticExportItem): self.__class__.instances.append(self) super().__init__() self.staticExportItem = staticExportItem self.parentTempoTrack = parentTempoTrack if not self.staticExportItem["positionInBlock"] == 0: self.setAcceptHoverEvents(True) self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable|QtWidgets.QGraphicsItem.ItemIsFocusable) self.setCursor(QtCore.Qt.SizeHorCursor) #this sets the cursor while the mouse is over the item. It is independent of AcceptHoverEvents else: self.ungrabMouse = api.nothing #to surpress a warning from the context menu self.note = QtWidgets.QGraphicsTextItem("") self.note.setParentItem(self) self.note.setFont(constantsAndConfigs.musicFont) self.note.setHtml("<font size='6'>{}</font>".format(constantsAndConfigs.realNoteDisplay[staticExportItem["referenceTicks"]])) self.note.setPos(-6,0) #adjust items font x offsset. self.number = QtWidgets.QGraphicsTextItem("") self.number.setParentItem(self) #self.number.setHtml("<font color='black' size='2'>{}</font>".format(str(int(staticExportItem["unitsPerMinute"])))) self.number.setHtml("<font size='2'>{}</font>".format(str(int(staticExportItem["unitsPerMinute"])))) self.number.setPos(-6,0) #adjust items font x offsset. if not self.staticExportItem["graphType"] == "standalone": self.arrow = QtWidgets.QGraphicsTextItem("?") #unicode long arrow right #http://xahlee.info/comp/unicode_arrows.html self.arrow.setParentItem(self) self.arrow.setPos(7,30) else: self.arrow = None for n in (self.note, self.number, self.arrow): if n: n.setDefaultTextColor(QtGui.QColor("black"))
def addIO(self): """Add/Update IO""" self.inputs = [] self.outputs = [] self.defineIO() indexOffset = 2 verticalIoSpacing = 20 # Vertical Space between 2 IOs verticalIoStartOffset = (verticalIoSpacing * (max(len(self.inputs), len(self.outputs)) + indexOffset)) / 2 horizontalIoDistance = 10 maxInputWidth = 0 maxOutputWidth = 0 nodeTitleWidth = self.nodeTitle.boundingRect().width() titlePadding = 10 horizontalTextToIoDistance = 5 for ioDefinition in self.inputs: if ioDefinition[0] is not None: if ioDefinition[0] is execType: ioDefinition.append(ExecInput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2])) else: ioDefinition.append(NodeInput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2])) ioDefinition.append(QGraphicsTextItem(ioDefinition[2], ioDefinition[3])) maxInputWidth = max(maxInputWidth, ioDefinition[4].boundingRect().width()) ioDefinition[4].setPos(QPointF(horizontalTextToIoDistance, - ioDefinition[3].boundingRect().height() / 2 - ioDefinition[4].boundingRect().height() / 4)) self.IO[ioDefinition[1]] = ioDefinition[3] for ioDefinition in self.outputs: if ioDefinition[0] is not None: if ioDefinition[0] is execType: ioDefinition.append(ExecOutput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2])) else: ioDefinition.append(NodeOutput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2])) ioDefinition.append(QGraphicsTextItem(ioDefinition[2], ioDefinition[3])) maxOutputWidth = max(maxInputWidth, ioDefinition[4].boundingRect().width()) ioDefinition[4].setPos(QPointF(- horizontalTextToIoDistance - ioDefinition[4].boundingRect().width(), - ioDefinition[3].boundingRect().height() / 2 - ioDefinition[4].boundingRect().height() / 4)) self.IO[ioDefinition[1]] = ioDefinition[3] width = max(maxInputWidth + maxOutputWidth + horizontalIoDistance, nodeTitleWidth + titlePadding) height = verticalIoSpacing * (max(len(self.inputs), len(self.outputs)) + indexOffset - 1) self.mainRect.setRect(QRectF(-width/2, -height/2, width, height)) for ioDefinition in self.inputs: if ioDefinition[0] is not None: ioDefinition[3].setPos(QPointF(-width/2, - verticalIoStartOffset + (self.inputs.index(ioDefinition) + indexOffset) * verticalIoSpacing)) for ioDefinition in self.outputs: if ioDefinition[0] is not None: ioDefinition[3].setPos(QPointF(width / 2, - verticalIoStartOffset + (self.outputs.index(ioDefinition) + indexOffset) * verticalIoSpacing)) self.nodeTitle.setPos(QPointF(- nodeTitleWidth / 2, -height / 2))