爲了學PySide我決定用它寫一個Sqlite3的管理工具,之後把源碼和心得再放上來,這兩天研究QFileDialog類,邊譯邊總結以下: html
功能調用函數 git
def acceptMode () github
def confirmOverwrite () app
def defaultSuffix () less
def directory () ide
def fileMode () 函數
def filter () 工具
def filters () ui
def history ()
def iconProvider ()
def isNameFilterDetailsVisible ()
def isReadOnly ()
def itemDelegate ()
def labelText (label)
def nameFilters ()
def open (receiver, member)
def options ()
def proxyModel ()
def resolveSymlinks ()
def restoreState (state)
def saveState ()
def selectFile (filename)
def selectFilter (filter)
def selectNameFilter (filter)
def selectedFiles ()
def selectedFilter ()
def selectedNameFilter ()
def setAcceptMode (mode)
def setConfirmOverwrite (enabled)
def setDefaultSuffix (suffix)
def setDirectory (directory)
def setDirectory (directory)
def setFileMode (mode)
def setFilter (filter)
def setFilter (filters)
def setFilters (filters)
def setHistory (paths)
def setIconProvider (provider)
def setItemDelegate (delegate)
def setLabelText (label, text)
def setNameFilter (filter)
def setNameFilterDetailsVisible (enabled)
def setNameFilters (filters)
def setOption (option[, on=true])
def setOptions (options)
def setProxyModel (model)
def setReadOnly (enabled)
def setResolveSymlinks (enabled)
def setSidebarUrls (urls)
def setViewMode (mode)
def sidebarUrls ()
def testOption (option)
def viewMode ()
信號
def currentChanged (path)
def directoryEntered (directory)
def fileSelected (file)
def filesSelected (files)
def filterSelected (filter)
def getExistingDirectory ([parent=None[, caption=」「[, dir=」「[, options=QFileDialog.ShowDirsOnly]]]])
def getOpenFileName ([parent=None[, caption=」「[, dir=」「[, filter=」「[, selectedFilter=」「[, options=0]]]]]])
def getOpenFileNames ([parent=None[, caption=」「[, dir=」「[, filter=」「[, selectedFilter=」「[, options=0]]]]]])
def getSaveFileName ([parent=None[, caption=」「[, dir=」「[, filter=」「[, selectedFilter=」「[, options=0]]]]]])
1.PySide.QtGui.QFileDialog類提供了一個對話框,容許用戶選擇的文件或目錄。
2.PySide.QtGui.QFileDialog類能夠讓用戶經過對話框選擇電腦文件系統的一個或多個文件或目錄。
3.最簡單的方法是使用PySide.QtGui.QFileDialog類的靜態函數:
例:fileName=QFileDialog.getOpenFileName(self,
"打開圖像文件", "/home/", "全部圖像文件 (*.png *.jpg *.bmp)" )
上例顯示的對話框,窗體的標題爲"打開圖像文件",默認路徑爲"/home/"
過濾器匹配爲"全部圖像文件 (*.png *.jpg *.bmp)" 表示只選擇擴展名爲png,jpg,bmp的文件,若是想使用多個過濾器,能夠用現個分號「;;」格開,例:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
4.自定義使用PySide.QtGui.QFileDialog類提供的文件對話框指定文件或目錄。
首先定一個實例
dialog = QFileDialog(self,」保存文檔」)
QFileDialog([parent=None[, caption=""[, directory=""[, filter=""]]]])
實例化類時可選參數有3個分別爲標題,當前目錄,過濾器。在本例中標題爲」保存文檔」
dialog.setFileMode(QFileDialog.AnyFile)
參數值 |
描述 |
QFileDialog.AnyFile |
肯定一個文件名,無論它是否存在 |
QFileDialog.ExistingFile |
肯定一個只能是存在的文件名 |
QFileDialog.Directory |
肯定一個目錄名. 文件和目錄都顯示 |
QFileDialog.ExistingFiles |
肯定零個或多個現有文件的名稱。 |
設置爲QFileDialog.AnyFile,這意味着用戶能夠選擇任何文件,或指定一個不存在的文件(自已填寫個呵)。經常使用於另存爲文件對話框。
設置爲QFileDialog.ExistingFile, 這意味着只能選擇文件系統存在的文件。經常使用於打開文件對話框。
dialog.setNameFilter("圖像文件 (*.png *.gif *.jpg)")
selectNameFilter (filter) 設置過濾器,用來篩選指定類型的文件
dialog.setViewMode(QFileDialog.Detail)
setViewMode (ViewMode) 設置瀏覽目錄或文件的方式方式有兩種: 列表展現和細節展現。
。ViewMode參數取值爲:
參數值 |
描述 |
QFileDialog.Detail |
顯示一個圖標,一個名稱,目錄中的每一項細節。 |
QFileDialog.List |
只顯示一個圖標和一個名爲每一個項目中的目錄。 |
dialog.setAcceptMode(QFileDialog.AcceptSave)
setAcceptMode (mode) 設置文件對話框是打開或保存文件對話框。 默認狀況下是打開對話框。
setDirectory() 設置當前目錄
setDefaultSuffix (suffix) 設置保存對話框默認的擴展名,如doc
例:dialog.setDefaultSuffix("db3")
setLabelText (label, text) 設置對話框對應的標籤值。
可對應更改標籤有:
QFileDialog.LookIn |
QFileDialog.FileName |
QFileDialog.FileType |
QFileDialog.Accept |
QFileDialog.Reject |
例:dialog.setLabelText(QFileDialog.LookIn,"路徑:")
dialog.setLabelText(QFileDialog.FileName,"文件名:")
dialog.setLabelText(QFileDialog.FileType,"文件類型:")
dialog.setLabelText(QFileDialog.Accept,"新建")
dialog.setLabelText(QFileDialog.Reject,"取消")
原:
改後:
setOptions (options) 配置選項。
setOption (option[, on=true])
option取值(吃飯了,這個就不詳說了):
參數 |
描述 |
QFileDialog.ShowDirsOnly |
Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.) |
QFileDialog.DontResolveSymlinks |
Don’t resolve symlinks in the file dialog. By default symlinks are resolved. |
QFileDialog.DontConfirmOverwrite |
Don’t ask for confirmation if an existing file is selected. By default confirmation is requested. |
QFileDialog.DontUseNativeDialog |
Don’t use the native file dialog. By default, the native file dialog is used unless you use a subclass of PySide.QtGui.QFileDialog that contains the Q_OBJECT() macro. |
QFileDialog.ReadOnly |
Indicates that the model is readonly. |
QFileDialog.HideNameFilterDetails |
Indicates if the file name filter details are hidden or not. |
QFileDialog.DontUseSheet |
In previous versions of Qt, the static functions would create a sheet by default if the static function was given a parent. This is no longer supported and does nothing in Qt 4.5, The static functions will always be an application modal dialog. If you want to use sheets, use QFileDialog.open() instead. |
if dialog.exec_():
fileNames = dialog.selectedFiles()
以上兩行代碼,動態創建了文件對話框。selectedFiles()屬性返回帶有路徑的文件名,fileNames類型爲python的List.。
其它功能詳見:http://srinikom.github.com/pyside-docs/modules.html