import sys
from PySide6.QtWidgets import QPushButton,QLabel,QProgressDialog,QHBoxLayout,QVBoxLayout,QWidget,QApplication,QMainWindow
from PySide6.QtCore import *
class QProgressDialogDemo(QMainWindow):
def __init__(self):
super(QProgressDialogDemo, self).__init__()
#设置窗口大小
self.resize(400, 150)
self.setWindowTitle("QProgressDialogDemo")
btn = QPushButton("开始")
btn.clicked.connect(self.btnClick)
#创建水平布局
layout = QHBoxLayout()
layout.addWidget(btn)
mainFrame = QWidget()
mainFrame.setLayout(layout)
self.setCentralWidget(mainFrame)
def btnClick(self):
elapsed = 200000
#self.progressDialog = QProgressDialog('下载进度','取消',0,elapsed,self)
self.progressDialog = QProgressDialog()
self.progressDialog.setLabelText("wait ...")
self.progressDialog.setRange(0,elapsed)
self.progressDialog.setCancelButton(None)
self.progressDialog.setWindowTitle('QProgressDialog')
self.progressDialog.setStyleSheet(u"QProgressBar::chunk\n"
"{\n"
"border-radius:11px;\n"
"background:qlineargradient(spread:pad,x1:0,y1:0,x2:1,y2:0,stop:0 #01FAFF,stop:1 #26B4FF);\n"
"}\n"
"QProgressBar#progressBar\n"
"{\n"
"height:22px;\n"
"text-align:center;/*\u6587\u672c\u4f4d\u7f6e*/\n"
"font-size:14px;\n"
"color:white;\n"
"border-radius:11px;\n"
"background: #1D5573 ;\n"
"}")
self.progressDialog.show()
for val in range(elapsed):
self.progressDialog.setValue(val)
QCoreApplication.processEvents()
if self.progressDialog.wasCanceled():
break
self.progressDialog.setValue(elapsed)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QProgressDialogDemo()
main.show()
sys.exit(app.exec_())
取消进度条中的calcel按钮
self.progressDialog.setCancelButton(None)
设置进度条的整体样式
self.progressDialog.setStyleSheet(u"QProgressBar::chunk\n"
"{\n"
"border-radius:11px;\n"
"background:qlineargradient(spread:pad,x1:0,y1:0,x2:1,y2:0,stop:0 #01FAFF,stop:1 #26B4FF);\n"
"}\n"
"QProgressBar#progressBar\n"
"{\n"
"height:22px;\n"
"text-align:center;/*\u6587\u672c\u4f4d\u7f6e*/\n"
"font-size:14px;\n"
"color:white;\n"
"border-radius:11px;\n"
"background: #1D5573 ;\n"
"}")