宽 "pen" 的 PyQt5 drawArc

PyQt5 drawArc with wide "pen"

我正在尝试使用 PyQt5 绘制圆规(MacOS 11.0.1,Python 3.9)。 我使用 drawArc 语句创建了仪表背景,因此我将笔宽设置为较大的值 (70)。产生的弧线看起来像马蹄铁,大概是因为“笔”是一个70像素的正方形,而不是垂直于行进方向的线。

有没有办法在 PyQt5 中创建圆弧,就像图片右侧的那样?

我愿意接受建议:该应用程序已经用 Python+Tkinter 编写,但由于 Tkinter+Raspberry 缺少抗锯齿功能,我需要重新编写它。

(计划 B 是继续使用 PyQt,创建一个饼图切片 (drawPie) 并用一圈背景颜色覆盖中心区域 - 但这并不理想,因为它对我的设计施加了一些限制。)

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

arcreading = 0
adder = .1
# creating a Gauge class
class Gauge(QMainWindow):

    # constructor
    def __init__(self):
        super().__init__()

        timer = QTimer(self)                 # create a timer object
        timer.timeout.connect(self.update)   # add action to the timer, update the whole code
        timer.start(0)                    # update cycle in milliseconds

        self.setGeometry(200, 200, 600, 600)       # window location and size
        self.setStyleSheet("background : black;")  # background color

    # -----------------------
    # method for paint event
    # -----------------------
    def paintEvent(self, event):
        global arcreading
        global adder
        # print('x')
        kanvasx = 50        # binding box origin: x
        kanvasy = 50        # binding box origin: y
        kanvasheight = 150  # binding box height
        kanvaswidth = 150   # binding box width
        arcsize = 270       # arc angle between start and end.
        arcwidth = 70       # arc width


        painter = QPainter(self)    # create a painter object
        painter.setRenderHint(QPainter.Antialiasing)  # tune up painter
        painter.setPen(QPen(Qt.green, arcwidth))      # set color and width

        # ---------- the following lines simulate sensor reading. -----------
        if arcreading > arcsize or arcreading < 0:  # variable to make arc move
            adder = -adder  # arcreading corresponds to the
            # value to be indicated by the arc.
        arcreading = arcreading + adder
        # --------------------- end simulation ------------------------------
        #print(arcreading)

        # drawArc syntax:
        #       drawArc(x_axis, y_axis, width, length, startAngle, spanAngle)
        painter.drawArc(kanvasx, kanvasy,   # binding box: x0, y0, pixels
                   kanvasheight + arcwidth, # binding box: height
                   kanvaswidth + arcwidth,  # binding box: width
                   int((arcsize + (180 - arcsize) / 2)*16),  # arc start point, degrees (?)
                   int(-arcreading*16))         # arc span

        painter.end()                       # end painter

    # Driver code
if __name__ == '__main__':
    app = QApplication(sys.argv)
    # creating a Gauge object
    win = Gauge()
    # show
    win.show()
    exit(app.exec_())

您需要设置 capStyle of the pen with the appropriate Qt.PenCapStyle,在您的情况下,您应该使用 FlatCap,它恰好在行尾结束,而默认值为 SquareCap(涵盖结束并延伸线宽的一半):

painter.setPen(QPen(Qt.green, arcwidth, cap=Qt.FlatCap))