本帖最后由 yanlusu 于 2024-6-19 20:31 编辑
[Python] 纯文本查看 复制代码 import sys
from PyQt6.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt6.QtCore import Qt, QRectF, QTimer
from PyQt6.QtGui import QPainter, QColor, QBrush, QTransform
class TiltedBlockProgressBar(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._value = 0
self._block_color = QColor('blue')
self._bg_color = QColor('lightgray')
self._block_width = 6
self._block_spacing = 3
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateProgress)
self.timer.start(500) # Update every 500 ms
def updateProgress(self):
self._value += 1
if self._value > 100:
self._value = 0
self.update()
def setBlockColor(self, color):
self._block_color = QColor(color)
self.update()
def setBackgroundColor(self, color):
self._bg_color = QColor(color)
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
# Draw background
painter.setBrush(QBrush(self._bg_color))
painter.setPen(Qt.PenStyle.NoPen)
painter.drawRect(self.rect())
if self._value > 0:
block_height = self.height()
total_blocks = min(self._value, 100)
for i in range(total_blocks):
x = i * (self._block_width + self._block_spacing)
# Create a transform for the tilt
transform = QTransform()
transform.translate(x + self._block_width / 2, block_height / 2)
transform.rotate(30)
transform.translate(-(x + self._block_width / 2), -block_height / 2)
painter.setTransform(transform)
# Draw each tilted block
block_rect = QRectF(x, 0, self._block_width, block_height)
painter.setBrush(QBrush(self._block_color))
painter.drawRect(block_rect)
# Reset transformation for next block
painter.resetTransform()
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
progressBar = TiltedBlockProgressBar()
progressBar.setBlockColor('blue')
progressBar.setBackgroundColor('lightgray')
progressBar.resize(300, 50)
layout.addWidget(progressBar)
window.setLayout(layout)
window.resize(300, 50)
window.show()
sys.exit(app.exec())
chatgpt:
[Asm] 纯文本查看 复制代码 Use qt6 to customize a progress bar. In the progress bar, multiple color blocks tilted at 30 degrees are used to indicate the progress. Each color block represents 1%. The width of the color block is 6 pixels, and the distance between the color blocks is 3 pixels.
|