PyQt QDialog - return value and close dialog box

I am working on a user interface in PyQt and am having problems using QDialog. Essentially, I have a main widget and a sub-widget stored in separate .py files; I would like the sub-widget to open when I click on a specific button in the main widget. This seems to be normal.

The problem is with return and closing. I have a send button in my subviewer - when the user clicks on this button, I would like to return the value (the dictionary made from their input) to the main widget and close the sub-widget. I cannot do any of this with the code that I have now.

Applicable code bits in the main widget (you can add more to make it standalone if the problem is not obvious):

import SGROIWidget_ui def retranslateUi(self, ROIGUI): #ShowGroupROI is a push-button self.ShowGroupROI.clicked.connect(self.ShowGroupROIFunction) def ShowGroupROIFunction(self): dialog = QDialog() dialog.ui = SGROIWidget_ui.Ui_ShowGroupWidget() dialog.ui.setupUi(dialog) dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) if dialog.exec_(): roiGroups=dialog.Submitclose() print(roiGroups) dialog.accept() 

It seems I never got into the code after the if-statement.

The applicable code from my sub-widget (there will be a bit more here):

 try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_ShowGroupWidget(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setupUi(self) def setupUi(self, ShowGroupWidget): #sets up Submit button def retranslateUi(self, ShowGroupWidget): self.Submit.clicked.connect(self.Submitclose) def Submitclose(self): roiGroups={} #roiGroups gets set up here as a dictionary #It prints nicely from here so I know it not the issue return roiGroups #I don't know if I can just do a return statement like this? self.close()* 

* I also tried ex.close (), but ex is not recognized when this widget starts as a dialog. It seems that it should not fall into this line due to the return statement, but I do not know how else to close this widget after the user clicks "send". Or should the .accept () dialog in my main widget handle this?

Last: do I need this in my sub-widget at all, because it launches through my main widget?

 if __name__=='__main__': app=QtGui.QApplication(sys.argv) ex=Ui_ShowGroupWidget() ex.show() sys.exit(app.exec_()) 

Thanks in advance! I'm new to PyQt, so hopefully this is somewhat legible.

+7
python user-interface qt pyqt qdialog
source share
1 answer

There are a few questions. String if dialog.exec_(): will succeed only if the dialog is completed using accept() . Do you work with QDesigner? If yes, check this out for a different way of working. If Ui_ShowGroupWidget just contains the code you are writing, you should make it inherit QDialog instead of QWidget. Then, instead of closing self.close() you close it with self.accept() . You cannot return diccionary, but you can save it as an attribute of an object. After returning dialog.exec_() you can access this attribute.

It could be something like this:

 def ShowGroupROIFunction(self): dialog = SGROIWidget_ui.Ui_ShowGroupWidget() if dialog.exec_(): print(dialog.roiGroups) 

Other:

 ... class Ui_ShowGroupWidget(QtGui.QDialog): def __init__(self): QtGui.QDialog.__init__(self) self.setupUi(self) self.roiGroups = {} self.Submit.clicked.connect(self.submitclose) def setupUi(self, ShowGroupWidget): #sets up Submit button def submitclose(self): #do whatever you need with self.roiGroups self.accept() 

Finally, if __name__=='__main__': means "if this file is executed as the main file, then ...", which is not so, since you include and use it from another. This way you can delete it, however the idea is that you can run python ui_mywidget.py to check it or see the Ui defined in this file

+12
source share

All Articles