Skip to content

Commit

Permalink
Merge pull request #85 from TomographicImaging/Cancel_button_default
Browse files Browse the repository at this point in the history
 When the Form Dialog's "Cancel" button is clicked, a function can be redefined to add additional functionality on "Cancel" and the dialog is closed by default
  • Loading branch information
DanicaSTFC authored Sep 29, 2023
2 parents 51f1261 + 44e23ba commit 9315808
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 11 additions & 0 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, parent=None, title=None):
self.setWindowTitle(title)
# add button box to the UI
self.formWidget.uiElements['verticalLayout'].addWidget(bb)
bb.button(QtWidgets.QDialogButtonBox.Cancel).clicked.connect(self._onCancel)

@property
def Ok(self):
Expand All @@ -34,6 +35,16 @@ def Cancel(self):
'''returns a reference to the Dialog Cancel button to connect its signals'''
return self.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel)

def _onCancel(self):
'''calls onCancel and closes the FormDialog'''
self.onCancel()
self.close()

def onCancel(self):
'''Called when the dialog's "Cancel" button is clicked.
Can be redefined to add additional functionality on "Cancel"'''
pass

@property
def widgets(self):
return self.formWidget.widgets
Expand Down
7 changes: 2 additions & 5 deletions examples/dialog_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ def __init__(self, parent=None):
def openFormDialog(self):
dialog = FormDialog(parent=self, title='Example')
dialog.Ok.clicked.connect(lambda: self.accepted())
dialog.Cancel.clicked.connect(lambda: self.rejected())

# ## Example on how to add elements to the
# Example on how to add elements to the FormDialog
# add input 1 as QLineEdit
qlabel = QtWidgets.QLabel(dialog.groupBox)
qlabel.setText("Input 1: ")
Expand All @@ -54,19 +53,17 @@ def openFormDialog(self):

# store a reference
self.dialog = dialog

self.dialog.onCancel = self.rejected
dialog.exec()

def accepted(self):
print("accepted")
print(self.dialog.widgets['input1_field'].text())
print(self.dialog.widgets['input2_field'].currentText())

self.dialog.close()

def rejected(self):
print("rejected")
self.dialog.close()


if __name__ == "__main__":
Expand Down

0 comments on commit 9315808

Please sign in to comment.