def paintEvent(self, event): super(BubbleLabel, self).paintEvent(event) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # 抗锯齿 rectPath = QPainterPath() # 圆角矩形 triPath = QPainterPath() # 底部三角形 height = self.height() - 8 # 往上偏移8 rectPath.addRoundedRect(QRectF(0, 0, self.width(), height), 5, 5) x = self.width() / 5 * 4 triPath.moveTo(x, height) # 移动到底部横线4/5处 # 画三角形 triPath.lineTo(x + 6, height + 8) triPath.lineTo(x + 12, height) rectPath.addPath(triPath) # 添加三角形到之前的矩形上 # 边框画笔 painter.setPen(QPen(self.BorderColor, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) # 背景画刷 painter.setBrush(self.BackgroundColor) # 绘制形状 painter.drawPath(rectPath) # 三角形底边绘制一条线保证颜色与背景一样 painter.setPen(QPen(self.BackgroundColor, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) painter.drawLine(x, height, x + 12, height)
def paint_waveform_arr(rect: QRect, wfm_arr: WFMArray) -> QPainterPath: rect_w = rect.width() rect_h = rect.height() w_scale = rect_w - 1 h_scale = rect_h - 1 vmin, vmax = -20, 120 vsize = vmax - vmin ypos = wfm_arr['ypos'] ypos = (wfm_arr['ypos'] * 100 / vmax * vsize - vmin) / vsize ypos_w = (ypos * h_scale - h_scale) * -1 wfm_range = (wfm_arr['ypos'].min(), wfm_arr['ypos'].max()) ypos_range = (ypos.min(), ypos.max()) ypos_w_range = (ypos_w.min(), ypos_w.max()) # print(f'{wfm_range=}, {ypos_range=}, {ypos_w_range=}, {rect_h=}') wfm_arr['ypos'] = ypos_w h, w = wfm_arr.shape wfm_arr['xpos'] *= w_scale xy_arr = rfn.structured_to_unstructured(wfm_arr[['xpos', 'ypos']]) paths = QPainterPath() for y in range(h): path = QPainterPath() y_arr = xy_arr[y] for x in range(w): xy = y_arr[x] if x > 0: path.lineTo(xy[0], xy[1]) else: path.moveTo(xy[0], xy[1]) paths.addPath(path) return paths