繁体   English   中英

PyQt:QtGui.QFileDialog.getSaveFileName在选择后不会关闭

[英]PyQt: QtGui.QFileDialog.getSaveFileName won't close after selection

在我的PyQt4应用程序中,有一项功能允许用户保存avi文件。 为此,在主窗口中实现了saveMovie方法:

def saveMovie(self):
    """ Let the user make a movie out of the current experiment. """
    filename = QtGui.QFileDialog.getSaveFileName(self, "Export Movie", "",
                                                 'AVI Movie File (*.avi)')

    if filename != "":
        dialog = QtGui.QProgressDialog('',
                                       QtCore.QString(),
                                       0, 100,
                                       self,
                                       QtCore.Qt.Dialog |
                                       QtCore.Qt.WindowTitleHint)

        dialog.setWindowModality(QtCore.Qt.WindowModal)
        dialog.setWindowTitle('Exporting Movie')
        dialog.setLabelText('Resampling...')

        dialog.show()

        make_movie(self.appStatus, filename, dialog)

        dialog.close()

我的想法是使用QProgressDialog来显示视频编码工作是如何进行的。
然而,在选择文件名后, QFileDialog不会消失,整个应用程序将保持无响应,直到make_movie函数完成。

我该怎么做才能避免这种情况?

获得的经验:如果您有一些长时间运行的操作 - 例如,读取或写入文件,将它们移动到另一个线程,或者它们将冻结UI。

因此,我创建了一个QThreadMovieMaker的子类,其run方法封装了make_movie实现的功能:

class MovieMaker(QThread):
    def __init__(self, uAppStatus, uFilename):
        QtCore.QThread.__init__(self, parent=None)
        self.appStatus = uAppStatus
        self.filename = uFilename

    def run(self):
        ## make the movie and save it on file

让我们回到saveMovie方法。 在这里,我使用以下代码替换了对make_movie的原始调用:

self.mm = MovieMaker(self.appStatus,
                     filename)

self.connect(self.mm, QtCore.SIGNAL("Progress(int)"),
             self.updateProgressDialog)

self.mm.start()

注意我是如何定义一个新信号 Progress(int)
MovieMaker线程发出这样的信号以更新用于向用户显示电影编码工作进展的QProgressDialog

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM