Reputation: 4086
I'm making a Dialog that returns a dictionary to a function. However the input needs to be validated before it can be processed.
I'm using the following code to run the Dialog and return the dictionary:
if transadd.exec_():
knowns = transadd.widg.get_values()
with widg being the Dialog and transadd being a widget holding the Dialog.
get_values simply runs through all the comboboxes and adds their values to a dictionary:
def get_values(self):
for key in self.unknowns:
self.unknowns[key] = self.unknown_trans[key][1].currentText()
return self.unknowns
Lastly I have a validation method which replaces is activated when a user clicks 'OK':
def validate(self, Dialog):
counting = 1
Errored = False
for key in self.unknowns:
if self.unknown_trans[key][1].currentText() == "Please Select..." and \
self.gridLayout.itemAtPosition(counting, 3) != 0:
self.gridLayout.addWidget(
QtGui.QLabel("Missing Answer", Dialog),
counting, 3, 1, 1
)
Errored = True
self.sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
counting += 1
if not Errored:
Dialog.accept()
Currently all the above works exactly how I want it to. The problem comes when the user clicks on the cancel Dialog (rejected()). In this case the validation doesn't occur because we are cancelling which is fine. But get_values is still run due to the first portion of code - which would in turn run the rest of the function and would create a inaccurate output.
So my question is: How can I make a Dialog box return values only when the user clicks accept as opposed to reject?
Upvotes: 0
Views: 1151
Reputation: 28684
In general, if you don't run a dialog in the standard manner (a top-level modal widget), you can still access whether it was accepted or rejected using the result:
transadd.widg.result()
Upvotes: 2