1.描述
QDialog是對話窗口的基類,對話窗口是頂級窗口,主要用于短期任務和與用戶的簡短通信。
可分為模態對話框和非模態對話框。
模態對話框又可以分為應用程序級別和窗口級別。
? 應用程序級別:當該種模態的對話框出現時,用戶必須首先對對話框進行交互,直到關閉對話框,然后才能訪問程序中其他的窗口
? 窗口級別:該模態僅僅阻塞與對話框關聯的窗口,但是依然允許用戶與程序中其它窗口交互
非模態對話框不會阻塞與對話框關聯的窗口以及與其他窗口進行交互
繼承自QWidget
2.功能作用
(1) 控件的創建
QDialog(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())
(2) 模態設置
setModal(bool)
modal() -> bool
(3) 彈出
open()
exec()
(4) 是否顯示尺寸調整控件
setSizeGripEnabled(bool)
isSizeGripEnabled() -> bool
(5) 常用操作槽
accept()
reject()
done(int r)
(6) 設置和獲取數值
setResult(int)
result() -> int
3.信號
accepted() # 接受
finished(int result) # 接受和拒絕都會發出該信號
rejected() # 拒絕
4.相關子類
(1) QFontDialog
(1)描述
? 提供了一種選擇字體的對話框控件,繼承自QDialog
(2)功能作用
# 構造函數
QFontDialog(parent: QWidget = None)
QFontDialog(QFont, parent: QWidget = None)# 打開對話框
open(self)
open(PYQT_SLOT) # 打開后, 會自動連接fontSelected信號與此處指定的槽函數
exec() -> int# 當前字體
setCurrentFont(QFont)
currentFont() -> QFont# 最終選中字體
selectedFont() -> QFont# 選項控制
setOption(QFontDialog.FontDialogOption, on=True)
"""on = True設置該選項on = False取消該選項
"""
setOptions(QFontDialog.FontDialogOption) # 設置多個選項
testOption(QFontDialog.FontDialogOption) # 測試某個選項是否生效
options() -> QFontDialog.FontDialogOption # 獲取當前的選項
"""
補充QFontDialog.FontDialogOptionQFontDialog.NoButtons不顯示“ 確定”和“ 取消”按鈕。(對“實時對話框”有用。)QFontDialog.DontUseNativeDialog在Mac上使用Qt的標準字體對話框而不是Apple的原生字體面板。QFontDialog.ScalableFonts顯示可縮放字體QFontDialog.NonScalableFonts顯示不可縮放的字體QFontDialog.MonospacedFonts顯示等寬字體QFontDialog.ProportionalFonts顯示比例字體
"""
# 靜態方法
getFont(parent: QWidget = None) -> Tuple[QFont, bool]
getFont(QFont, parent: QWidget = None, caption: str = '', options: QFontDialog.FontDialogOption) -> Tuple[QFont, bool]
"""參數1: 默認字體2. 父控件3. 對話框標題4. 選項返回值(最終字體, 是否點擊確認)
"""
(3)信號
currentFontChanged(QFont) # 當前字體發生改變時
fontSelected(QFont) # 最終選擇字體時
(2) QColorDialog
(1)描述
? 顏色對話框的功能是允許用戶選擇顏色,繼承自QDialog
(2)功能作用
# 構造函數
QColorDialog(parent: QWidget = None)
QColorDialog(Union[QColor, Qt.GlobalColor, QGradient], parent: QWidget = None)# 打開對話框
open(self)
open(PYQT_SLOT)打開后, 會自動連接colorSelected信號與此處指定的槽函數
exec() -> int# 當前顏色
setCurrentColor(QColor())
currentColor() -> QColor# 最終選中顏色
selectedColor()# 選項控制
setOption(self, QColorDialog.ColorDialogOption, on: bool = True)
setOptions(self, Union[QColorDialog.ColorDialogOptions, QColorDialog.ColorDialogOption])
testOption(self, QColorDialog_ColorDialogOption)
"""
QColorDialog.ColorDialogOptionQColorDialog.ShowAlphaChannel允許用戶選擇顏色的alpha分量。QColorDialog.NoButtons不顯示“ 確定”和“ 取消”按鈕。(對“實時對話框”有用。)QColorDialog.DontUseNativeDialog使用Qt的標準顏色對話框而不是操作系統原生顏色對話框。
"""# 靜態方法
customCount() -> int
setCustomColor(int index, QColor color)
customColor(int index) -> QColor
setStandardColor(int index, QColor color)
standardColor(int index) -> QColor
getColor(initial: Union[QColor, Qt.GlobalColor, QGradient] = Qt.white, parent: QWidget = None, title: str = '', options: Union[QColorDialog.ColorDialogOptions, QColorDialog.ColorDialogOption] = QColorDialog.ColorDialogOptions()) -> QColor
(3)信號
colorSelected(QColor color)
currentColorChanged(QColor color)
(3) QFileDialog
(1)描述
? 提供了一個對話框,允許用戶選擇文件或目錄。繼承自QDialog
(2)功能作用
# 最簡單的獲取方式(靜態方法)
# 獲取文件
getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[str, str]getOpenFileNames(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[List[str], str]getOpenFileUrl(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0, supportedSchemes: Iterable[str] = []) -> Tuple[QUrl, str]getOpenFileUrls(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0, supportedSchemes: Iterable[str] = []) -> Tuple[List[QUrl], str]getSaveFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[str, str]getSaveFileUrl(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0, supportedSchemes: Iterable[str] = []) -> Tuple[QUrl, str]
"""
過濾字符串格式:名稱1(*.jpg *.png);;名稱2(*.py)
"""# 獲取文件夾
getExistingDirectory(parent: QWidget = None, caption: str = '', directory: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly) -> str
getExistingDirectoryUrl(parent: QWidget = None, caption: str = '', directory: QUrl = QUrl(), options: Union[QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly, supportedSchemes: Iterable[str] = []) -> QUrl# 構造函數
QFileDialog(QWidget, Union[Qt.WindowFlags, Qt.WindowType])
QFileDialog(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '')
L
# 接收模式
acceptMode() -> QFileDialog.AcceptMode
setAcceptMode(QFileDialog.AcceptMode)
"""QFileDialog.AcceptModeQFileDialog.AcceptOpen打開QFileDialog.AcceptSave保存
"""# 默認后綴
setDefaultSuffix(str)
defaultSuffix() -> str#設置文件模式
setFileMode(QFileDialog.FileMode)
fileMode() -> QFileDialog.FileMode
"""
QFileDialog.FileModeQFileDialog.AnyFile文件的名稱,無論是否存在。QFileDialog.ExistingFile單個現有文件的名稱。QFileDialog.Directory目錄的名稱。顯示文件和目錄。但是,本機Windows文件對話框不支持在目錄選擇器中顯示文件。QFileDialog.ExistingFiles零個或多個現有文件的名稱。
"""# 設置名稱過濾器
setNameFilter(str)
setNameFilters(str)# 顯示信息的詳細程度
setViewMode(QFileDialog.ViewMode)
viewMode() -> QFileDialog.ViewMode
"""
QFileDialog.ViewModeQFileDialog.DetailQFileDialog.List
"""# 設置指定角色的標簽名稱
setLabelText(self, QFileDialog.DialogLabel, str)
"""QFileDialog.FileNameQFileDialog.AcceptQFileDialog.RejectQFileDialog.FileTypeQFileDialog.LookIn
"""# 打開對話框
open(self)
open(PYQT_SLOT) # 打開后, 會自動連接 filesSelected 信號與此處指定的槽函數
exec() -> int
(3)信號
currentChanged(path_str) # 當前路徑發生改變時
currentUrlChanged(QUrl) # 當前路徑url發生改變時
directoryEntered(directory_str) # 打開選中文件夾時
directoryUrlEntered(QUrl directory) # 打開選中文件夾url時
filterSelected(filter_str) # 選擇名稱過濾器時
fileSelected(str) # 最終選擇文件時
filesSelected([str]) # 選擇多個文件時
urlSelected(QUrl url) # 最終選擇url時
urlsSelected(List[QUrl]) # 最終選擇多個url時
(4) QInputDialog
(1)描述
? 提供了一個簡單方便的對話框,獲得來自用戶的單個值
? 輸入值可以是字符串,數字或列表中的項目
? 設置標簽以告知用戶應輸入的內容
? 繼承自QDialog
(2)功能作用
# 常用的靜態方法
getInt(QWidget, str, str, value: int = 0, min: int = -2147483647, max: int = 2147483647, step: int = 1, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()) -> Tuple[int, bool]getDouble(QWidget, str, str, value: float = 0, min: float = -2147483647, max: float = 2147483647, decimals: int = 1, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()) -> Tuple[float, bool]getDouble(QWidget, str, str, float, float, float, int, Union[Qt.WindowFlags, Qt.WindowType], float) -> Tuple[float, bool]getText(QWidget, str, str, echo: QLineEdit.EchoMode = QLineEdit.Normal, text: str = '', flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags(), inputMethodHints: Union[Qt.InputMethodHints, Qt.InputMethodHint] = Qt.ImhNone) -> Tuple[str, bool]getMultiLineText(QWidget, str, str, text: str = '', flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags(), inputMethodHints: Union[Qt.InputMethodHints, Qt.InputMethodHint] = Qt.ImhNone) -> Tuple[str, bool]getItem(QWidget, str, str, Iterable[str], current: int = 0, editable: bool = True, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags(), inputMethodHints: Union[Qt.InputMethodHints, Qt.InputMethodHint] = Qt.ImhNone) -> Tuple[str, bool]# 構造函數
QInputDialog(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())# 選項設置
setOption(self, QInputDialog.InputDialogOption, on: bool = True)
setOptions(self, Union[QInputDialog.InputDialogOptions, QInputDialog.InputDialogOption])
testOption(self, QInputDialog.InputDialogOption) -> bool
options(self) -> QInputDialog.InputDialogOptions
"""QInputDialog.InputDialogOptionQInputDialog.NoButtons不顯示“ 確定”和“ 取消”按鈕(對“實時對話框”有用)。QInputDialog.UseListViewForComboBoxItems使用QListView而不是不可編輯的QComboBox來顯示使用setComboBoxItems()設置的項目。QInputDialog.UsePlainTextEditForTextInput使用QPlainTextEdit進行多行文本輸入。該值在5.2中引入。
"""# 輸入模式
inputMode(self) -> QInputDialog.InputMode
setInputMode(self, QInputDialog.InputMode)
"""
QInputDialog.InputModeTextInputIntInputDoubleInput
"""# 界面文本設置
setLabelText(str)
labelText(self) -> str
setOkButtonText(str)
setCancelButtonText(str)# 各個小分類設置
# 整型
setIntMaximum(self, int)
intMaximum(self) -> int
setIntMinimum(self, int)
intMinimum(self) -> int
setIntRange(self, int, int)
setIntStep(self, int)
intStep(self) -> int
setIntValue(self, int)
intValue(self) -> int
# 浮點型
setDoubleMaximum(self, float)
doubleMaximum() -> float
setDoubleDecimals(self, int)
doubleDecimals() -> int
setDoubleMinimum(self, float)
doubleMinimum(self) -> float
setDoubleRange(self, float, float)
setDoubleStep(self, float)
doubleStep(self) -> float
setDoubleValue(self, float)
doubleValue(self) -> float
# 字符串
setTextEchoMode(self, QLineEdit.EchoMode)
textEchoMode(self) -> QLineEdit.EchoMode
setTextValue(self, str)
textValue(self) -> str
# 下拉列表
setComboBoxItems(self, Iterable[str])
comboBoxItems(self) -> List[str]
setComboBoxEditable(self, bool)
isComboBoxEditable(self) -> bool
(3)信號
intValueChanged(int value)
intValueSelected(int value)
doubleValueChanged(double value)
doubleValueSelected(double value)
textValueChanged(text_str)
textValueSelected(text_str)