Using diag.exec_()
will block until the dialog returns (is closed). So, if you will need to call show()
on it's own. There are a few ways to proceed from here.
- You can have the dialog accept slot run a reference to the rest of the commands
- You can poll the dialog to see if the user has accepted
- You can move the
pre
,run
, andpost
commands to the dialog
Assuming you want to keep the meat of the code out of the dialog class, and since periodically polling is best to avoid if possible, here is an example of the first strategy:
import sys, time
from PyQt4 import QtCore, QtGui
class MyDialog(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
layout = QtGui.QVBoxLayout()
self.msg = QtGui.QLabel('some sort of status')
self.buttonbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok, QtCore.Qt.Horizontal, self)
self.buttonbox.accepted.connect(self.accept)
layout.addWidget(self.msg)
layout.addWidget(self.buttonbox)
self.setLayout(layout)
def set_msg(self, new_msg):
self.msg.setText(new_msg)
def set_function_on_accept(self,fcn):
self.function = fcn
def accept(self):
self.function()
class Command(object):
def do(self):
self.setupUi()
def do_work(self):
self.pre()
self.run()
self.post()
def pre(self):
# do setup stuff for run method
time.sleep(1)
self.diag.set_msg("stuff setup")
QtGui.QApplication.processEvents()
def run(self):
# do actual work
time.sleep(1)
self.diag.set_msg("work done")
QtGui.QApplication.processEvents()
def post(self):
# clean up after run
time.sleep(1)
self.diag.set_msg("cleaned up")
QtGui.QApplication.processEvents()
def setupUi(self):
# create a ui for this command
diag = MyDialog()
self.diag = diag
diag.set_function_on_accept(self.do_work)
diag.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
command = Command()
command.do()
sys.exit(app.exec_())