首页 文章

PyQt QDialog - 返回一个值并从对话框中关闭

提问于
浏览
7

我正在使用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 回答

  • 12

    有一些问题 . 只有在使用 accept() 退出对话框时, if dialog.exec_(): 行才会成功 . 你在使用QDesigner吗?如果是这样,请检查this以获得不同的工作方式 . 如果 Ui_ShowGroupWidget 只包含你编写的代码,你应该让它继承QDialog而不是QWidget . 然后,不是用 self.close() 关闭它,而是用 self.accept() 关闭它 . 您不能返回一个diccionary,但您可以将其保存为对象属性 . 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__': 表示"if this file is executed as the main file, then..",情况并非如此,因为您要包含并使用另一个 . 所以你可以删除它,但是,想法是你可以运行 python ui_mywidget.py 来测试它或看到在该文件上定义的Ui

相关问题