一半君的总结纸

听话只听一半君

QFileDialog 如何才能设置一个默认文件并默认滚动到此默认文件处

试了半天不管是用static method 还是 instance版本的,都最多只能设置lineedit处的text,后来用了这个脑残法

就是找到树形结构的treeView, 然后硬设过去… 会闪一下,然后有点延迟。感觉这属于乱搞,求正确方法…

import os,sys

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt


def _browse_file_dialog(mode=0,title='select file',filter="Executable Files (*);;json Files (*.json)", default_file=None,parent=None):
    '''
    browse a file with default file name selected in treeview and lineedit
    mode  0  load
          1  save
    '''
    if parent is None:
        app = QtGui.QApplication(sys.argv)

    dialog = QtGui.QFileDialog(parent)
    dialog.setWindowTitle(title)
    if mode==0:
        dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
    elif mode==1:
        dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)

    dialog.setFilter(filter)

    # customization to select default file
    dialogTreeView = dialog.findChild(QtGui.QTreeView)
    model  = dialogTreeView.model()

    if default_file is not None:
        if os.path.isfile(default_file):

            dialog.selectFile(default_file)
            QtCore.QTimer.singleShot(0, lambda: dialogTreeView.setCurrentIndex(model.index(default_file))  )
            QtCore.QTimer.singleShot(0, lambda: QtGui.qApp.processEvents()  )
            QtCore.QTimer.singleShot(0, lambda: dialogTreeView.scrollTo(model.index(default_file)))

    file_selected = None
    if dialog.exec_():
        file_selected = str(dialog.selectedFiles()[0])

    if file_selected == 'None':
        file_selected = None

    return file_selected


if __name__ == '__main__':
    _browse_file_dialog(default_file = os.path.expandvars('/usr/lib64/libogg.so.0'))

特意找了一个文件比较多的文件夹,以说明他会闪一下(即有延迟),文件少的时候几乎察觉不到。

file browser dialog with default

文件少的时候
file browser dealy with fewer files

ps:我又试了一下,好像 getOpenFileName getSaveFileName的时候不用像上面那样,能选到默认的文件,(除了“滚动到”默认的文件那里这个效果没有),不知道上次试的时候为什么不行

#! /usr/bin/env python2
import sys

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt

class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        default = "/usr/lib/libphonon.so.4"
        fileName = QtGui.QFileDialog.getOpenFileName(self, "Open File", default, "Any Files (*)")
        print 'selected fileName:', fileName

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv + ['-graphicssystem', 'raster'])
    gui = MyDialog()
    # gui.show()
    app.exec_()

file open dialog default is ok

edit: lz脑残了,在家里的CentOS 6.5和7.0上都试了下,根本没上面的问题,直接传默认文件作为参数即可,有可能是公司的sb系统pyqt版本过低,我记得好象是4.6.1。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: