PyQt QDialog return response yes or no

后端 未结 1 1745
失恋的感觉
失恋的感觉 2021-01-18 15:11

I have a QDialog class

confirmation_dialog = uic.loadUiType(\"ui\\\\confirmation_dialog.ui\")[0]

class ConfirmationDialog(QDialog,confirmation_dialog):

           


        
相关标签:
1条回答
  • 2021-01-18 15:53

    You would use dialog.exec_(), which will open the dialog in a modal, blocking mode and returns an integer that indicates whether the dialog was accepted or not. Generally, instead of emitting signals, you probably just want to call self.accept() or self.reject() inside the dialog to close it.

    dialog = ConfirmationDialog()
    result = dialog.exec_()
    if result:  # accepted
        return 'accepted'
    

    If I'm using a dialog to get a specific set of values from the user, I'll usually wrap it in a staticmethod, that way I can just call it and get values back within the control flow of my application, just like a normal function.

    class MyDialog(...)
    
        def getValues(self):
            return (self.textedit.text(), self.combobox.currentText())
    
        @staticmethod
        def launch(parent):
            dlg = MyDialog(parent)
            r = dlg.exec_()
            if r:
                return dlg.getValues()
            return None
    
    values = MyDialog.launch(None)
    

    However, in almost all cases where I just need to present a message to the user, or get them to make a choice by clicking a button, or I need them to input a small piece of data, I can use the built-in static methods on the normal dialog classes -- QMessageBox, QInputDialog

    0 讨论(0)
提交回复
热议问题