PyQt QFileDialog exec_很慢 [英] PyQt QFileDialog exec_ is slow

查看:295
本文介绍了PyQt QFileDialog exec_很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义QFileDialog,因为我想选择多个目录. 但是exec_函数非常慢,我不知道为什么.我正在使用最新版本的PyQt.

I'm using a custom QFileDialog because I want to select multiple directories. But the exec_ function is very slow, and I can't figure out why. I'm using the newest version of PyQt.

代码段:

from PyQt4 import QtGui, QtCore, QtNetwork, uic

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi('gui.ui', self)            
        self.connect(self.multiPackerAddDirsBtn,
                     QtCore.SIGNAL('clicked()'), self.multiPackerAddDirs)

    def multiPackerAddDirs(self):
        dialog = QtGui.QFileDialog(self)
        dialog.setFileMode(QtGui.QFileDialog.Directory)
        dialog.setOption(QtGui.QFileDialog.ShowDirsOnly, True)
        dialogTreeView = dialog.findChild(QtGui.QTreeView)
        dialogTreeView.setSelectionMode(
            QtGui.QAbstractItemView.ExtendedSelection)
        if dialog.exec_():
            for dirname in dialog.selectedFiles():
                self.multiPackerDirList.addItem(str(dirname))
                print(str(dirname))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

推荐答案

QFileDialog构造函数创建一个Qt对话框,而静态函数(例如getSaveFileName)将创建一个本机对话框(除非

The QFileDialog constructor creates a Qt dialog, whereas the static functions (like getSaveFileName) will create a native one (unless the DontUseNativeDialog option is set to True).

根据使用的平台,本机对话框可能比Qt更快或更慢.

The native dialogs may be faster or slower than Qt's, depending on the platform in use.

对于某些平台,问题似乎更为严重.请参见此长期存在的错误,该问题会影响Windows XP和Windows 7(以及其他) Qt 4.7/4.8.

For some plaforms, though, it appears the problem may be more acute. See this longstanding bug, which affects Windows XP and Windows 7 (amongst others) with Qt 4.7 / 4.8.

更新

请明确说明:

在Windows上,静态函数 QFileDialog.getExistingDirectory 打开本机的浏览文件夹"对话框,该对话框仅允许选择一个目录.因此,Qt无法提供一个 native 对话框来选择多个目录,因为Windows没有提供该对话框.

On Windows, the static function QFileDialog.getExistingDirectory opens the native "Browse For Folder" dialog, which only allows selecting a single directory. So Qt cannot provide a native dialog for selecting multiple directories, because Windows doesn't provide one.

另一种主要选择是按照

The other main alternative is to use Qt's own, non-native file-dialog and monkey-patch it as suggested in this faq. However, as you've already discovered, this currently has the significant downside of being annoyingly slow due to bugs in the underlying implementation.

剩下的唯一选择是编写您自己的目录列表对话框,或者尝试考虑解决当前问题的另一种方法(即,不使用文件对话框).

The only remaining alternatives are to either write your own directory-lister dialog, or try to think of another way of solving your immediate problem (i.e. without using a file-dialog).

这篇关于PyQt QFileDialog exec_很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆