吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 651|回复: 6
收起左侧

[求助] qt绘制倾斜带间隔的进度条

[复制链接]
swico 发表于 2024-6-19 17:13
求助大佬们,这种怎么在painting里面绘制啊,用qpainter绘制,qprogressbar的qss不支持倾斜绘制
进度条3 效果.jpg

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yanlusu 发表于 2024-6-19 20:29
本帖最后由 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.


image.png



xixicoco 发表于 2024-6-20 01:24
龍謹 发表于 2024-6-20 07:00
anorith 发表于 2024-6-20 09:12
学习一下,后面也会学到qt
 楼主| swico 发表于 2024-6-20 11:49
yanlusu 发表于 2024-6-19 20:29
[mw_shl_code=python,true]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++转一下就可以。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 14:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表