PySide6 GUI 编程(28):QProgressDialog进度条

2024-08-21 10:25:21 浏览数 (3)

示例代码

代码语言:python代码运行次数:0复制
from __future__ import annotations

import sys

from PySide6.QtCore import Qt, QTimer
from PySide6.QtWidgets import QApplication, QMainWindow, QProgressDialog, QPushButton, QVBoxLayout, QWidget


class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.progress = None
        self.setWindowTitle("QProgressDialog Demo")

        self.minimum_value = 0
        self.maximum_value = 100
        self.current_value = 0

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.progress_dialog_update)
        self.timer.setTimerType(Qt.TimerType.PreciseTimer)
        self.timer.setInterval(20)  # 每20毫秒触发一次

        self.begin_button = QPushButton("开始运行")
        self.begin_button.clicked.connect(self.progress_dialog_show)

        v_layout = QVBoxLayout()
        v_layout.addWidget(self.begin_button)

        container = QWidget()
        container.setLayout(v_layout)

        self.setCentralWidget(container)

    def progress_dialog_show(self):
        self.progress = QProgressDialog(self)
        self.progress.setWindowTitle("请稍候")
        self.progress.setLabelText("正在处理...")
        self.progress.setMinimum(self.minimum_value)
        self.progress.setMaximum(self.maximum_value)
        self.progress.setValue(self.minimum_value)
        self.progress.setModal(True)
        self.progress.setCancelButtonText("取消执行")
        # self.progress.setCancelButton(None) # 隐藏按钮, 不展示此按钮
        self.progress.canceled.connect(self.progress_canceled)
        self.progress.show()
        self.timer.start()

    def progress_canceled(self):
        print('用户取消了进度对话框, 当前已完成进展:{:.2f}%'.format(self.current_value / self.maximum_value * 100))
        self.progress.setValue(self.maximum_value)
        self.timer.stop()
        self.progress.accept()
        self.current_value = self.minimum_value

    def progress_dialog_update(self):
        if self.progress.value()   1 > self.current_value:
            self.current_value = self.progress.value()   1
        # 当进度值等于最大值时,QProgressDialog会将进度值重置为-1
        # 因此无法直接判断progress.value() >= self.maximum_value
        # 需要判断progress.value() < self.minimum_value或progress.value() >= self.progress.maximum() - 1
        if self.progress.value() < self.minimum_value or self.progress.value() >= self.progress.maximum() - 1:
            self.current_value = self.minimum_value
            self.progress.setValue(self.maximum_value)
            self.timer.stop()
            self.progress.accept()
            print('进度对话框已完成')
            return
        self.progress.setValue(self.progress.value()   1)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyMainWindow()
    window.show()
    sys.exit(app.exec())

运行效果

进度条运行效果进度条运行效果

特别注意

当进度值累加到最大值或超过最大值时,QProgressDialog会将进度值重置为-1

代码语言:python代码运行次数:0复制
if __name__ == "__main__":
    app = QApplication(sys.argv)

    # 测试QProgressDialog在最大值时,会将进度值重置为-1
    test_progress_dialog = QProgressDialog()
    test_progress_dialog.setMinimum(0)
    test_progress_dialog.setMaximum(10)
    # 当进度值等于最大值时,QProgressDialog会将进度值重置为-1
    # 在进度值达到最大值时自动完成并关闭
    test_progress_dialog.setValue(10)
    print(test_progress_dialog.value())

    sys.exit(app.exec())

0 人点赞