QThread basic

 1 from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar, QVBoxLayout
 2 from PySide2.QtCore import QThread, Signal, Qt
 3 import time
 4 
 5 class myThread(QThread):
 6     change_value = Signal(int)
 7 
 8     def run(self):
 9         ct = 0
10         while ct < 100:
11             ct += 1
12             time.sleep(0.3)
13             self.change_value.emit(ct)
14 
15 
16 class MyWidget(QWidget):
17     def __init__(self):
18         super(MyWidget, self).__init__()
19         self.setGeometry( 500, 500, 650, 450)
20         self.setWindowTitle("My Progress bar")
21         self.initUI()
22         self.show()
23 
24     def initUI(self):
25         # Create ProgressBar
26         self.pgb = QProgressBar()
27         self.pgb.setStyleSheet("QProgressBar {border: 2px solid grey; border-radius: 8px; padding:1px}"
28                                "QProgressBar::chunk {background:green}")
29         #self.pgb.setOrientation(Qt.Vertical)   # set vertical progress bar
30         #self.pgb.setTextVisible(False)         # hide text
31 
32         self.startButn = QPushButton("Start")
33         self.startButn.setStyleSheet("">self.startButn.clicked.connect(self.startProgressBar)
34 
35         vbox = QVBoxLayout()
36         vbox.addWidget(self.pgb)
37         vbox.addWidget(self.startButn)
38         self.setLayout(vbox)
39 
40     def startProgressBar(self):
41         self.thread = myThread()
42         self.thread.change_value.connect(self.setProgressVal)
43         self.thread.start()
44 
45     def setProgressVal(self, val):
46         self.pgb.setValue(val)
47 
48 
49 
50 if __name__ == "__main__":
51     app = QApplication([])
52     widget = MyWidget()
53 
54 
55     app.exit(app.exec_())

 

上一篇:使用PySide2 QGraphicsView,QGraphicsScene 制作鼠标绘图板


下一篇:python基础14-__enter__/__exit__/描述符管理实例的实参类型控制/类的装饰器/自定制property/自定义元类