swico 发表于 2024-6-19 17:13

qt绘制倾斜带间隔的进度条

求助大佬们,这种怎么在painting里面绘制啊,用qpainter绘制,qprogressbar的qss不支持倾斜绘制

yanlusu 发表于 2024-6-19 20:29

本帖最后由 yanlusu 于 2024-6-19 20:31 编辑

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:
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.





xixicoco 发表于 2024-6-20 01:24

非常的牛叉的效果哦

龍謹 发表于 2024-6-20 07:00

一楼的是借用了一下ChatGPT吗?

anorith 发表于 2024-6-20 09:12

学习一下,后面也会学到qt

swico 发表于 2024-6-20 11:49

yanlusu 发表于 2024-6-19 20:29
import sys
from PyQt6.QtWidgets import QWidget, QApplication, QVBoxLayout
...

学习一下,打算写个cpp版本的

yanlusu 发表于 2024-6-22 10:38

swico 发表于 2024-6-20 11:49
学习一下,打算写个cpp版本的

直接chatgpt,让它用c++转一下就可以。
页: [1]
查看完整版本: qt绘制倾斜带间隔的进度条