PyQt中QFileDialog记录上次打开目录(历史目录)的方法

使用QtGui的标准文件类QFileDialog来获取打开文件名或文件列表时,会使用到静态函数getOpenFileName()或者getOpenFileNames()函数。

官方文档中该函数的具体声明是:
  • QString  getOpenFileName (QWidget  parent = None, QString  caption = QString(), QString  directory = QString(), QString  filter = QString(), Options  options = 0)
  • QString  getOpenFileName (QWidget  parent = None, QString  caption = QString(), QString  directory = QString(), QString  filter = QString(), QString  selectedFilter = None, Options  options = 0)
  • (QString, QString)  getOpenFileNameAndFilter (QWidget  parent = None, QString  caption = QString(), QString  directory = QString(), QString  filter = QString(), QString  initialFilter = QString(), Options  options = 0)
  • QStringList  getOpenFileNames (QWidget  parent = None, QString  caption = QString(), QString  directory = QString(), QString  filter = QString(), Options  options = 0)
  • QStringList  getOpenFileNames (QWidget  parent = None, QString  caption = QString(), QString  directory = QString(), QString  filter = QString(), QString  selectedFilter = None, Options  options = 0)
在网上找了一些例子,都是忽略了selectedFilter和Options这两项,而directory这项设为一个指定的目录,一般为'./'当前目录。
这样就会导致一个问题,就是在需要使用文件对话框时,每次打开都是程序当前目录,而你想要的文件可能都在一个目录,但一般不在程序目录,所以每次都得手动找。
那么有没有解决办法呢?答案是肯定的,因为我们在使用其他程序时,打开文件对话框,都会默认打开上次使用的目录,也就是说它自带历史记录。
百度一下,有人说,当 directory设为空字符串( ‘’),就会记录上次打开的文件。试了一下,没有成功。
于是google一下,在 Stack Overflow上找到了这个问题的答案。

If you omit the dir argument (or pass in an empty string), the dialog should remember the last directory:

    filename = QtGui.QFileDialog.getOpenFileName(
                   parent, 'Open File', '', 'Images (*.png *.xpm *.jpg)')

The tr function is used for translating user-visible strings. You can omit it if you won't ever be providing translations for your application.

EDIT:

It seems that the start directory may not be automatically remembered on all platforms/desktops, depending on whether you use the native dialog or not. If Qt's built-in dialog is used, the start directory should always be automatically remebered on all platforms (even between invokations of the application). To try the non-native dialog, do:

    filename = QtGui.QFileDialog.getOpenFileName(
                   parent, 'Open File', '', 'Images (*.png *.xpm *.jpg)',None, QtGui.QFileDialog.DontUseNativeDialog)

Alternatively, you can use the QFileDialog constructor, which will always create a non-native dialog:

    dialog = QtGui.QFileDialog(parent)
    dialog.setWindowTitle('Open File')
    dialog.setNameFilter('Images (*.png *.xpm *.jpg)')
    dialog.setFileMode(QtGui.QFileDialog.ExistingFile)if dialog.exec_() == QtGui.QDialog.Accepted:
        filename = dialog.selectedFiles()[0]

 
原来是native dialog在作怪,需要在options参数中设置为 QtGui . QFileDialog . DontUseNativeDialog  。改掉代码运行,发现程序报错,说是第5个参数,也就是 selectedFilter 不能为NoneTpye,再看官方文档,对 selectedFilter的讲解不是很清楚,但是它的静态函数有两个版本,一个是没有 selectedFilter参数的,难道默认用的是这个函数。将这个参数项去掉,运行一遍,果然就好使了。
filename = QtGui.QFileDialog.getOpenFileName(
                   parent, 'Open File', '', 'Images (*.png *.xpm *.jpg)',QtGui.QFileDialog.DontUseNativeDialog)

总结起来,QFileDialog要使用历史记录,有三个需要注意的地方:
1、 directory设为空字符串( ‘’);
2、要使用参数少的那个函数方法,即不含有 selectedFilter;
3、最后一个参数(options)要设为 QtGui . QFileDialog . DontUseNativeDialog 。









  • 11
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值