-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqprogressbar_example.py
More file actions
123 lines (84 loc) · 3.68 KB
/
qprogressbar_example.py
File metadata and controls
123 lines (84 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import time
from PySide2 import QtCore
from PySide2 import QtWidgets
from shiboken2 import wrapInstance
import maya.cmds as cmds
import maya.OpenMayaUI as omui
def maya_main_window():
"""
Return the Maya main window widget as a Python object
"""
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)
class ProgressTestDialog(QtWidgets.QDialog):
WINDOW_TITLE = "Progress Test"
def __init__(self, parent=maya_main_window()):
super(ProgressTestDialog, self).__init__(parent)
self.setWindowTitle(self.WINDOW_TITLE)
if cmds.about(ntOS=True):
self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)
elif cmds.about(macOS=True):
self.setWindowFlags(QtCore.Qt.Tool)
self.setMinimumSize(300, 100)
self.test_is_running = False
self.create_widgets()
self.create_layout()
self.create_connections()
self.update_visibility()
def create_widgets(self):
self.progress_bar_label = QtWidgets.QLabel("Operation Progress")
self.progress_bar = QtWidgets.QProgressBar()
self.progress_bar_button = QtWidgets.QPushButton("Do It!")
self.cancel_button = QtWidgets.QPushButton("Cancel")
def create_layout(self):
progress_layout = QtWidgets.QVBoxLayout()
progress_layout.addWidget(self.progress_bar_label)
progress_layout.addWidget(self.progress_bar)
button_layout = QtWidgets.QHBoxLayout()
button_layout.addStretch()
button_layout.addWidget(self.progress_bar_button)
button_layout.addWidget(self.cancel_button)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.setContentsMargins(2, 2, 2, 2)
main_layout.addLayout(progress_layout)
main_layout.addStretch()
main_layout.addLayout(button_layout)
def create_connections(self):
self.progress_bar_button.clicked.connect(self.run_progress_test)
self.cancel_button.clicked.connect(self.cancel_progress_test)
def update_visibility(self):
self.progress_bar.setVisible(self.test_is_running)
self.progress_bar_label.setVisible(self.test_is_running)
self.progress_bar_button.setHidden(self.test_is_running)
self.cancel_button.setVisible(self.test_is_running)
def run_progress_test(self):
if self.test_is_running:
return
QtCore.QCoreApplication.processEvents()
number_of_operations = 10
self.progress_bar.setRange(0, number_of_operations)
self.progress_bar.setValue(0)
self.test_is_running = True
self.update_visibility()
for i in range(1, number_of_operations + 1):
if not self.test_is_running:
break
self.progress_bar_label.setText('Processing step {} of {}'.format(i, number_of_operations))
self.progress_bar.setValue(i)
time.sleep(0.5)
QtCore.QCoreApplication.processEvents()
self.test_is_running = False
self.update_visibility()
def cancel_progress_test(self):
if not self.test_is_running:
return
self.test_is_running = False
self.update_visibility()
if __name__ == "__main__":
try:
test_dialog.close() # pylint: disable=E0601
test_dialog.deleteLater()
except:
pass
test_dialog = ProgressTestDialog()
test_dialog.show()