PyQt QDialog - 返回值和关闭对话框

14
我正在使用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无法被识别。由于返回语句,似乎不应该到达这一行,但是我不知道在用户点击“提交”后如何关闭此小部件。或者我的主小部件中的dialog.accept()是否应处理它?
最后一件事 - 我是否需要在子小部件中使用它,因为它是通过我的主小部件运行的?
if __name__=='__main__':
    app=QtGui.QApplication(sys.argv)
    ex=Ui_ShowGroupWidget()
    ex.show()
    sys.exit(app.exec_())

提前感谢!我对PyQt还比较陌生,希望这篇文章能够让人容易理解。

1个回答

21

有几个问题。在if dialog.exec_():这行代码中,只有当使用accept()退出对话框时才会成功。你正在使用QDesigner吗?如果是,请查看此处以了解不同的工作方式。如果Ui_ShowGroupWidget仅包含您编写的代码,则应该使其继承自QDialog而不是QWidget。然后,关闭它的方式应该改为使用self.accept()而不是self.close()。你不能返回一个字典,但可以将它保存为对象属性。一旦dialog.exec_()返回,你就可以访问那个属性。

可能像这样:

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

另一个:

...

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()

最后,if __name__=='__main__':表示“如果这个文件作为主文件执行,那么...”,但在您从另一个文件中包含和使用它时并非如此。因此,您可以将其删除,但是您仍然可以运行python ui_mywidget.py来测试或查看在该文件中定义的Ui。


2
你让我开心了 ^^ - PAYRE Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接