PyQt5自定义组件之信号强度

此控件用于展示信号强度.

效果如下:

from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPainter, QPen
from PyQt5.Qt import Qt


class ShowSignals(QWidget):
    # 显示信号强度的控件
    def __init__(self, parent=None):
        super(ShowSignals, self).__init__(parent)

    def paintEvent(self, QPaintEvent):
        # 模拟信号强度(0-100)
        import random
        signal_num = random.randint(0, 100)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)  # 绘制图像反锯齿
        painter.begin(self)
        self.draw(painter, signal_num)
        painter.end()

    def draw(self, painter, signal_num):
        if signal_num == 0:
            pen_red = QPen(Qt.red, 4, Qt.SolidLine)
            painter.setPen(pen_red)
            painter.drawLine(10, 30, 30, 10)
            painter.drawLine(10, 10, 30, 30)
        else:
            green_count = signal_num // 25 + 1
            white_count = 4 - green_count
            pen_white = QPen(Qt.white, 6, Qt.SolidLine)
            pen_green = QPen(Qt.green, 6, Qt.SolidLine)
            painter.setPen(pen_green)
            for i in range(green_count):
                painter.drawLine(10 * (i + 1), 30, 10 * (i + 1), 25 - i * 5)
            painter.setPen(pen_white)
            for j in range(white_count):
                painter.drawLine(10 * (green_count + j + 1), 30, 10 * (green_count + j + 1), 25 - (green_count + j) * 5)
posted @ 2020-04-09 10:30  怀心抱素  阅读(453)  评论(0编辑  收藏  举报