-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogue_box.py
109 lines (92 loc) · 4.02 KB
/
dialogue_box.py
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
from PySide2 import QtCore
from PySide2 import QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
# Get the main Maya window
def mayaMainWindow():
mainWindowPtr = omui.MQtUtil.mainWindow()
return wrapInstance(int(mainWindowPtr), QtWidgets.QWidget)
# Creating own signal for QLineEdit
class MyLineEdit(QtWidgets.QLineEdit):
enter_pressed = QtCore.Signal(str)
def keyPressEvent(self, e):
super(MyLineEdit, self).keyPressEvent(e)
if e.key() == QtCore.Qt.Key_Enter or e.key() == QtCore.Qt.Key_Return:
self.enter_pressed.emit("Enter Key Pressed")
elif e.key() == QtCore.Qt.Key_Return:
self.enter_pressed.emit("Return Key Pressed")
class TestDialogue(QtWidgets.QDialog):
# Constructor (make parent the Maya main window so exists in Maya window and doesn't create a new window outside Maya window)
def __init__(self, parent=mayaMainWindow()):
super(TestDialogue, self).__init__(parent)
self.setWindowTitle("Test Dialogue")
self.setMinimumWidth(200)
# The help button is disabled by default in Maya 2022
#self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)
# Call these functions to instantiate widgets in window (along with layouts and connections for widgets)
self.createWidgets()
self.createLayouts()
self.createConnections()
def createWidgets(self):
# Combo box (drop down box)
self.combobox = QtWidgets.QComboBox()
self.combobox.addItems(["ComboBoxItem 1", "ComboBoxItem 2", "ComboBoxItem 3", "ComboBoxItem 4"])
# Line edit (text field)
self.linedit = MyLineEdit()
# Check Box
self.checkbox1 = QtWidgets.QCheckBox()
self.checkbox2 = QtWidgets.QCheckBox()
# Buttons
self.okBtn = QtWidgets.QPushButton("OK")
self.cancelBtn = QtWidgets.QPushButton("Cancel")
def createLayouts(self):
# Form Layout
formLayout = QtWidgets.QFormLayout()
formLayout.addRow("ComboBox: ", self.combobox)
formLayout.addRow("Name: ", self.linedit)
formLayout.addRow("Hidden: ", self.checkbox1)
formLayout.addRow("Lock: ", self.checkbox2)
# Horizontal Box Layout
buttonLayout = QtWidgets.QHBoxLayout()
buttonLayout.addStretch()
buttonLayout.addWidget(self.okBtn)
buttonLayout.addWidget(self.cancelBtn)
# Vertical Box Layout for main layout (to hold other layouts)
mainLayout = QtWidgets.QVBoxLayout(self)
mainLayout.addLayout(formLayout)
mainLayout.addLayout(buttonLayout)
# Create connections for signals to slots
def createConnections(self):
self.linedit.enter_pressed.connect(self.on_enter_pressed)
self.checkbox1.toggled.connect(self.printIsHidden)
self.combobox.activated.connect(self.on_activated_int)
self.combobox.activated[str].connect(self.on_activated_str) #[str] to indicate which function to call using decorator
self.cancelBtn.clicked.connect(self.close)
# Slots for different widgets
def on_enter_pressed(self, text):
print(text)
# Decorators for "function overloading" (since python doesn't do python overloading)
@QtCore.Slot(int)
def on_activated_int(self, index):
print("ComboBox Index: {0}".format(index))
@QtCore.Slot(str)
def on_activated_str(self, text):
print("ComboBox Text: {0}".format(text))
def printHelloName(self, name):
#name = self.linedit.text()
print("Hello {0}!".format(name))
def printIsHidden(self, checked):
#isHidden = self.checkbox1.isChecked()
if checked:
print("Object is hidden")
else:
print("Object is visible")
# Main function of script
if __name__ == "__main__":
try:
testDialogue.close()
testDialogue.deleteLater()
except:
pass
testDialogue = TestDialogue();
testDialogue.show()