我正在PyQt中开发一个用户界面,在尝试使用QDialog时遇到了一些问题。本质上,我有一个主小部件和一个子小部件,保存在单独的.py文件中;我希望子小部件在单击主小部件中的某个按钮时打开。这看起来开得不错。
问题在于退货和结案。我的子窗口小部件上有一个“提交”按钮-当用户单击此按钮时,我想将一个值(从他们的输入中生成的字典)返回到主窗口小部件,并关闭子窗口小部件。我现在的代码似乎不能做这两件事。
主小部件中的适用代码位(如果问题不明显,可以添加更多代码使其独立):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()
我好像从来没有在if语句之后执行过代码。
我的子窗口小部件中的适用代码(这里将包括更多内容):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's not the issue
return roiGroups
#I don't know if I can just do a return statement like this?
self.close()*
*我也在这里尝试过ex.close(),但是当这个小部件作为对话框运行时,无法识别ex。因为return语句,它似乎不应该到达这一行,但是我不知道在用户点击“submit”之后如何关闭这个小部件。或者我的主小部件中的dialog.accept()应该处理这个问题吗?
最后一件事-我是否需要在我的子窗口小部件中使用它,因为它是通过我的主窗口小部件运行的?if __name__=='__main__':
app=QtGui.QApplication(sys.argv)
ex=Ui_ShowGroupWidget()
ex.show()
sys.exit(app.exec_())
提前谢谢!我对PyQt还不太熟悉,所以希望这有点易读。