import sys
import time
from PySide6.QtWidgets import QApplication, QMainWindow, QProgressBar, QVBoxLayout, QWidget, QPushButton
from PySide6.QtCore import QThread, Signal


class WorkerThread(QThread):
    progress_updated = Signal(int)  # 进度更新信号
    finished = Signal()  # 完成信号

    def run(self):
        # 模拟耗时操作
        for i in range(1, 101):
            time.sleep(0.05)  # 模拟耗时操作
            self.progress_updated.emit(i)  # 发射进度信号
        self.finished.emit()


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("独立进度条示例")
        self.setGeometry(100, 100, 400, 200)
        
        # 创建进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setRange(0, 100)
        
        # 创建开始按钮
        self.start_button = QPushButton("开始加载")
        self.start_button.clicked.connect(self.start_loading)
        
        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.progress_bar)
        layout.addWidget(self.start_button)
        
        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)
        
        # 初始化工作线程
        self.worker_thread = WorkerThread()
        self.worker_thread.progress_updated.connect(self.update_progress)
        self.worker_thread.finished.connect(self.loading_finished)
    
    def start_loading(self):
        self.start_button.setEnabled(False)
        self.worker_thread.start()
    
    def update_progress(self, value):
        self.progress_bar.setValue(value)
    
    def loading_finished(self):
        self.start_button.setEnabled(True)
        # 这里可以添加加载完成后的操作


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