def paintEvent(self, event): painter = QPainter(self) painter.save() path = QPainterPath() painter.setFont(self.font) painter.setRenderHint(QPainter.Antialiasing) pen = QPen(QColor(0, 0, 0, 230)) pen_width = 3 pen.setWidth(pen_width) len = self.metrics.width(self.txt) w = self.width() px = (len - w) / 2 if px < 0: px = -px py = (self.height() - self.metrics.height()) / 2 + self.metrics.ascent() if py < 0: py = -py path.addText(px + 2, py + 2, self.font, self.txt) painter.strokePath(path, pen) painter.drawPath(path) painter.fillPath(path, QBrush(self.color)) painter.restore()
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): view = self.scene().views()[0] view_width = view.mapToScene(0, 0, self.width, 1).boundingRect().width() width, unit = self.getWidthAndUnit(view_width) # Current scale text = f"{width * self.units[self.unit] / self.units[unit]:.3g} {unit}" width = width * view.transform().m11() fm = QtGui.QFontMetrics(self.font, painter.device()) path = QtGui.QPainterPath() path.addText( self.width / 2.0 - fm.boundingRect(text).width() / 2.0, fm.ascent(), self.font, text, ) painter.strokePath(path, QtGui.QPen(QtCore.Qt.black, 2.0)) painter.fillPath(path, QtGui.QBrush(self.color, QtCore.Qt.SolidPattern)) # Draw the bar rect = QtCore.QRectF(self.width / 2.0 - width / 2.0, fm.height(), width, self.height) painter.setPen(QtGui.QPen(QtCore.Qt.black, 1.0)) painter.setBrush(QtGui.QBrush(self.color, QtCore.Qt.SolidPattern)) painter.drawRect(rect)
def drawText(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) font: QFont = QFont() font.setBold(True) font.setPointSize(10) painter.setFont(font) now: QDateTime = QDateTime.currentDateTime() fm: QFontMetricsF = QFontMetricsF(font) textList: List[AnyStr] = [ now.toString("MM月dd日yyyy"), now.toString("hh:mm:ss.zzz") ] # 绘制文本路径 textPath: QPainterPath = QPainterPath() textPath.addText(-fm.width(textList[0]) / 2.0, -fm.lineSpacing() / 2.0, font, textList[0]) textPath.addText(-fm.width(textList[1]) / 2.0, fm.lineSpacing() / 2.0, font, textList[1]) strokeColor: QColor = self.__textColor.light(80) strokeColor.setAlphaF(0.2) painter.strokePath( textPath, QPen(strokeColor, self.__shadowWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) painter.setBrush(self.__textColor) painter.drawPath(textPath) painter.restore()
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): fm = QtGui.QFontMetrics(self.font, painter.device()) path = QtGui.QPainterPath() path.addText(0, fm.ascent(), self.font, self.text()) painter.strokePath(path, QtGui.QPen(QtCore.Qt.black, 2.0)) painter.fillPath(path, QtGui.QBrush(self.color, QtCore.Qt.SolidPattern))
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): width = painter.viewport().width() fm = QtGui.QFontMetrics(self.font, painter.device()) rect = QtCore.QRect(0, fm.height(), width, self.height) painter.drawPixmap(rect, self.pixmap) painter.setPen(QtGui.QPen(QtCore.Qt.black, 2.0)) painter.setBrush(QtGui.QBrush(QtCore.Qt.white, QtCore.Qt.NoBrush)) painter.drawRect(rect) path = QtGui.QPainterPath() # Todo: find a better pad value path.addText( width - fm.boundingRect(self.unit).width() - 10, fm.ascent(), self.font, self.unit, ) vrange = self.vmax - self.vmin if vrange <= 0.0: return for value in self.niceTextValues(7, trim=1): x = width * (value - self.vmin) / vrange text = f"{value:.6g}" path.addText( x - fm.boundingRect(text).width() / 2.0, fm.ascent(), self.font, text, ) if self.checkmarks: path.addRect( x - fm.lineWidth() / 2.0, fm.ascent() + fm.underlinePos(), fm.lineWidth() * 2.0, fm.underlinePos(), ) painter.strokePath(path, QtGui.QPen(QtCore.Qt.black, 2.0)) painter.fillPath(path, QtGui.QBrush(self.color, QtCore.Qt.SolidPattern))
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): fm = QtGui.QFontMetrics(self.font, painter.device()) y = fm.ascent() for text, color in zip(self._texts, self.colors): path = QtGui.QPainterPath() path.addText(0, y, self.font, text) painter.strokePath(path, QtGui.QPen(QtCore.Qt.black, 2.0)) painter.fillPath(path, QtGui.QBrush(color, QtCore.Qt.SolidPattern)) y += fm.height()