内容表

上一话题

QColumnView

下一话题

QCommandLinkButton

QComboBox

QComboBox widget is a combined button and popup list. 更多

Inheritance diagram of PySide2.QtWidgets.QComboBox

继承者: QFontComboBox

概要

函数

虚函数

信号

详细描述

../../_images/windows-combobox.png

A QComboBox provides a means of presenting a list of options to the user in a way that takes up the minimum amount of screen space.

组合框是显示当前项的选择 Widget,且可以弹出可选择项的列表。组合框可能是可编辑的,使用户能够修改每列表项。

组合框可以包含像素图及字符串; insertItem() and setItemText() functions are suitably overloaded. For editable comboboxes, the function clearEditText() is provided, to clear the displayed string without changing the combobox’s contents.

会发射 3 个信号若组合框当前项改变, currentIndexChanged() , currentTextChanged() and activated() . currentIndexChanged() and currentTextChanged() are always emitted regardless if the change was done programmatically or by user interaction, while activated() is only emitted when the change is caused by user interaction. The highlighted() signal is emitted when the user highlights an item in the combobox popup list. All three signals exist in two versions, one with a QString 自变量和一个带有 int 自变量。若用户选择或突显像素图,仅 int 信号被发射。每当可编辑组合框文本改变时 editTextChanged() 信号被发射。

当用户在可编辑组合框中录入新字符串时,可能插入或可能不插入 Widget,且可以将 Widget 插入在多个位置。默认策略是 InsertAtBottom 但可以改变这使用 setInsertPolicy() .

可以将输入约束到可编辑组合框使用 QValidator ;见 setValidator() . By default, any input is accepted.

可以使用插入函数填充组合框, insertItem() and insertItems() for example. Items can be changed with setItemText() . An item can be removed with removeItem() and all items can be removed with clear() . The text of the current item is returned by currentText() , and the text of a numbered item is returned with text(). The current item can be set with setCurrentIndex() . The number of items in the combobox is returned by count() ; the maximum number of items can be set with setMaxCount() . You can allow editing using setEditable() . For editable comboboxes you can set auto-completion using setCompleter() and whether or not the user can add duplicates is set with setDuplicatesEnabled() .

QComboBox 使用 模型/视图框架 用于其弹出列表并存储其项。默认情况下 QStandardItemModel 存储项和 QListView 子类显示弹出列表。可以直接访问模型和视图 (采用 model() and view() ), but QComboBox also provides functions to set and get item data (e.g., setItemData() and itemText() ). You can also set a new model and view (with setModel() and setView() ). For the text and icon in the combobox label, the data in the model that has the DisplayRole and DecorationRole 在使用。注意:不可以更改 SelectionMode view() , e.g., by using setSelectionMode() .

class QComboBox ( [ parent=None ] )
param parent

QWidget

构造组合框采用给定 parent ,使用默认模型 QStandardItemModel .

PySide2.QtWidgets.QComboBox. InsertPolicy

此枚举指定什么是 QComboBox 应该做的,当用户键入新字符串时。

常量

描述

QComboBox.NoInsert

字符串不会插入组合框。

QComboBox.InsertAtTop

字符串将插入作为第一组合框项。

QComboBox.InsertAtCurrent

当前项将 replaced 通过字符串。

QComboBox.InsertAtBottom

将字符串插入在组合框最后项之后。

QComboBox.InsertAfterCurrent

将字符串插入在组合框当前项之后。

QComboBox.InsertBeforeCurrent

将字符串插入在组合框当前项之前。

QComboBox.InsertAlphabetically

按字母次序将字符串插入组合框。

PySide2.QtWidgets.QComboBox. SizeAdjustPolicy

此枚举指定尺寸提示应如何 QComboBox 调节当添加新内容或改变内容时。

常量

描述

QComboBox.AdjustToContents

组合框始终根据内容调节

QComboBox.AdjustToContentsOnFirstShow

组合框将调节到其首次展示内容。

QComboBox.AdjustToMinimumContentsLengthWithIcon

组合框将调节到 minimumContentsLength 加图标空间。出于性能原因,在大型模型中使用此策略。

PySide2.QtWidgets.QComboBox. activated ( arg__1 )
参数

arg__1 – unicode

注意

此函数被弃用。

PySide2.QtWidgets.QComboBox. activated ( index )
参数

index int

PySide2.QtWidgets.QComboBox. addItem ( icon , text [ , userData=None ] )
参数
  • icon QIcon

  • text – unicode

  • userData – object

把项添加到组合框,采用给定 icon and text ,和包含指定 userData (存储在 UserRole )。项会被追加到现有项列表。

PySide2.QtWidgets.QComboBox. addItem ( text [ , userData=None ] )
参数
  • text – unicode

  • userData – object

把项添加到组合框,采用给定 text ,和包含指定 userData (存储在 UserRole )。项会被追加到现有项列表。

PySide2.QtWidgets.QComboBox. addItems ( texts )
参数

texts – 字符串列表

添加每字符串在给定 texts 到组合框。每项会被依次追加到现有项列表中。

PySide2.QtWidgets.QComboBox. autoCompletion ( )
返回类型

bool

注意

此函数被弃用。

使用 completer() 代替。

PySide2.QtWidgets.QComboBox. autoCompletionCaseSensitivity ( )
返回类型

CaseSensitivity

注意

此函数被弃用。

使用 setCompleter() and setCaseSensitivity() 代替。

PySide2.QtWidgets.QComboBox. clear ( )

清零组合框,移除所有项。

注意:若组合框有设置外部模型,此模型仍会被清零 (当调用此函数时)。

PySide2.QtWidgets.QComboBox. clearEditText ( )

清零用于在组合框中编辑的行编辑内容。

PySide2.QtWidgets.QComboBox. completer ( )
返回类型

QCompleter

返回被用于自动补全组合框文本输入的补全器。

另请参阅

setCompleter() editable

PySide2.QtWidgets.QComboBox. count ( )
返回类型

int

PySide2.QtWidgets.QComboBox. currentData ( [ role=Qt.UserRole ] )
参数

role int

返回类型

object

PySide2.QtWidgets.QComboBox. currentIndex ( )
返回类型

int

另请参阅

setCurrentIndex()

PySide2.QtWidgets.QComboBox. currentIndexChanged ( arg__1 )
参数

arg__1 – unicode

注意

此函数被弃用。

PySide2.QtWidgets.QComboBox. currentIndexChanged ( index )
参数

index int

PySide2.QtWidgets.QComboBox. currentText ( )
返回类型

unicode

另请参阅

setCurrentText()

PySide2.QtWidgets.QComboBox. currentTextChanged ( arg__1 )
参数

arg__1 – unicode

PySide2.QtWidgets.QComboBox. duplicatesEnabled ( )
返回类型

bool

PySide2.QtWidgets.QComboBox. editTextChanged ( arg__1 )
参数

arg__1 – unicode

PySide2.QtWidgets.QComboBox. findData ( data [ , role=Qt.UserRole [ , flags=static_cast<Qt.MatchFlags>(Qt.MatchExactly|Qt.MatchCaseSensitive) ] ] )
参数
  • data – object

  • role int

  • flags MatchFlags

返回类型

int

返回索引当项包含给定 data 为给定 role ;否则返回 -1。

flags 指定如何搜索组合框项。

PySide2.QtWidgets.QComboBox. findText ( text [ , flags=static_cast<Qt.MatchFlags>(Qt.MatchExactly|Qt.MatchCaseSensitive) ] )
参数
  • text – unicode

  • flags MatchFlags

返回类型

int

返回索引当项包含给定 text ;否则返回 -1。

flags 指定如何搜索组合框项。

PySide2.QtWidgets.QComboBox. hasFrame ( )
返回类型

bool

PySide2.QtWidgets.QComboBox. hidePopup ( )

Hides the list of items in the combobox if it is currently visible and resets the internal state, so that if the custom pop-up was shown inside the reimplemented showPopup() , then you also need to reimplement the function to hide your custom pop-up and call the base class implementation to reset the internal state whenever your custom pop-up widget is hidden.

另请参阅

showPopup()

PySide2.QtWidgets.QComboBox. highlighted ( arg__1 )
参数

arg__1 – unicode

注意

此函数被弃用。

PySide2.QtWidgets.QComboBox. highlighted ( index )
参数

index int

PySide2.QtWidgets.QComboBox. iconSize ( )
返回类型

QSize

另请参阅

setIconSize()

PySide2.QtWidgets.QComboBox. initStyleOption ( option )
参数

option QStyleOptionComboBox

初始化 option 采用值来自此 QComboBox 。此方法对子类是有用的,当需要 QStyleOptionComboBox , but don’t want to fill in all the information themselves.

另请参阅

initFrom()

PySide2.QtWidgets.QComboBox. inputMethodQuery ( query , argument )
参数
  • query InputMethodQuery

  • argument – object

返回类型

object

PySide2.QtWidgets.QComboBox. insertItem ( index , icon , text [ , userData=None ] )
参数
  • index int

  • icon QIcon

  • text – unicode

  • userData – object

插入 icon , text and userData (存储在 UserRole ) 进组合框在给定 index .

If the index is equal to or higher than the total number of items, the new item is appended to the list of existing items. If the index is zero or negative, the new item is prepended to the list of existing items.

另请参阅

insertItems()

PySide2.QtWidgets.QComboBox. insertItem ( index , text [ , userData=None ] )
参数
  • index int

  • text – unicode

  • userData – object

插入 text and userData (存储在 UserRole ) 进组合框在给定 index .

If the index is equal to or higher than the total number of items, the new item is appended to the list of existing items. If the index is zero or negative, the new item is prepended to the list of existing items.

另请参阅

insertItems()

PySide2.QtWidgets.QComboBox. insertItems ( index , texts )
参数
  • index int

  • texts – 字符串列表

插入字符串从 list 到组合框作为单独项,起始于 index 指定。

If the index is equal to or higher than the total number of items, the new items are appended to the list of existing items. If the index is zero or negative, the new items are prepended to the list of existing items.

另请参阅

insertItem()

PySide2.QtWidgets.QComboBox. insertPolicy ( )
返回类型

InsertPolicy

另请参阅

setInsertPolicy()

PySide2.QtWidgets.QComboBox. insertSeparator ( index )
参数

index int

将分隔符项插入组合框在给定 index .

If the index is equal to or higher than the total number of items, the new item is appended to the list of existing items. If the index is zero or negative, the new item is prepended to the list of existing items.

另请参阅

insertItem()

PySide2.QtWidgets.QComboBox. isEditable ( )
返回类型

bool

PySide2.QtWidgets.QComboBox. itemData ( index [ , role=Qt.UserRole ] )
参数
  • index int

  • role int

返回类型

object

返回数据为给定 role 以给定 index in the combobox, or Invalid 若此角色没有数据。

另请参阅

setItemData()

PySide2.QtWidgets.QComboBox. itemDelegate ( )
返回类型

QAbstractItemDelegate

返回由弹出列表视图使用的项委托。

另请参阅

setItemDelegate()

PySide2.QtWidgets.QComboBox. itemIcon ( index )
参数

index int

返回类型

QIcon

返回图标为给定 index 在组合框。

另请参阅

setItemIcon()

PySide2.QtWidgets.QComboBox. itemText ( index )
参数

index int

返回类型

unicode

返回文本为给定 index 在组合框。

另请参阅

setItemText()

PySide2.QtWidgets.QComboBox. lineEdit ( )
返回类型

QLineEdit

返回用于编辑组合框项的行编辑,若 None 若没有行编辑。

仅可编辑组合框拥有行编辑。

另请参阅

setLineEdit()

PySide2.QtWidgets.QComboBox. maxCount ( )
返回类型

int

另请参阅

setMaxCount()

PySide2.QtWidgets.QComboBox. maxVisibleItems ( )
返回类型

int

PySide2.QtWidgets.QComboBox. minimumContentsLength ( )
返回类型

int

PySide2.QtWidgets.QComboBox. model ( )
返回类型

QAbstractItemModel

返回由组合框使用的模型。

另请参阅

setModel()

PySide2.QtWidgets.QComboBox. modelColumn ( )
返回类型

int

另请参阅

setModelColumn()

PySide2.QtWidgets.QComboBox. placeholderText ( )
返回类型

unicode

PySide2.QtWidgets.QComboBox. removeItem ( index )
参数

index int

移除项位于给定 index 从组合框。这将更新当前索引,若索引被移除。

此函数什么都不做,若 index 超出范围。

PySide2.QtWidgets.QComboBox. rootModelIndex ( )
返回类型

QModelIndex

返回组合框项的根模型项索引。

PySide2.QtWidgets.QComboBox. setAutoCompletion ( enable )
参数

enable bool

注意

此函数被弃用。

使用 setCompleter() 代替。

另请参阅

autoCompletion()

PySide2.QtWidgets.QComboBox. setAutoCompletionCaseSensitivity ( sensitivity )
参数

sensitivity CaseSensitivity

注意

此函数被弃用。

使用 setCompleter() and setCaseSensitivity() 代替。

PySide2.QtWidgets.QComboBox. setCompleter ( c )
参数

c QCompleter

设置 completer 以使用而不是当前补全器。若 completer is None ,自动补全被禁用。

默认情况下,对于可编辑组合框, QCompleter 自动创建不区分大小写的内联补全。

注意

补全器被移除,当 editable 特性变为 false 。设置补全器在 QComboBox (不可编辑) 会被忽略。

另请参阅

completer()

PySide2.QtWidgets.QComboBox. setCurrentIndex ( index )
参数

index int

另请参阅

currentIndex()

PySide2.QtWidgets.QComboBox. setCurrentText ( text )
参数

text – unicode

另请参阅

currentText()

PySide2.QtWidgets.QComboBox. setDuplicatesEnabled ( enable )
参数

enable bool

PySide2.QtWidgets.QComboBox. setEditText ( text )
参数

text – unicode

设置 text in the combobox’s text edit.

PySide2.QtWidgets.QComboBox. setEditable ( editable )
参数

editable bool

另请参阅

isEditable()

PySide2.QtWidgets.QComboBox. setFrame ( arg__1 )
参数

arg__1 bool

另请参阅

hasFrame()

PySide2.QtWidgets.QComboBox. setIconSize ( size )
参数

size QSize

另请参阅

iconSize()

PySide2.QtWidgets.QComboBox. setInsertPolicy ( policy )
参数

policy InsertPolicy

另请参阅

insertPolicy()

PySide2.QtWidgets.QComboBox. setItemData ( index , value [ , role=Qt.UserRole ] )
参数
  • index int

  • value – object

  • role int

设置数据 role 为项在给定 index 在组合框到指定 value .

另请参阅

itemData()

PySide2.QtWidgets.QComboBox. setItemDelegate ( delegate )
参数

delegate QAbstractItemDelegate

设置项 delegate 为弹出列表视图。组合框拥有委托的所有权。

警告

You should not share the same instance of a delegate between comboboxes, widget mappers or views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() signal, and attempt to access, modify or close an editor that has already been closed.

另请参阅

itemDelegate()

PySide2.QtWidgets.QComboBox. setItemIcon ( index , icon )
参数
  • index int

  • icon QIcon

设置 icon 为项在给定 index 在组合框。

另请参阅

itemIcon()

PySide2.QtWidgets.QComboBox. setItemText ( index , text )
参数
  • index int

  • text – unicode

设置 text 为项在给定 index 在组合框。

另请参阅

itemText()

PySide2.QtWidgets.QComboBox. setLineEdit ( edit )
参数

edit QLineEdit

设置行 edit 以代替使用当前行编辑 Widget。

组合框拥有行编辑的所有权。

另请参阅

lineEdit()

PySide2.QtWidgets.QComboBox. setMaxCount ( max )
参数

max int

另请参阅

maxCount()

PySide2.QtWidgets.QComboBox. setMaxVisibleItems ( maxItems )
参数

maxItems int

另请参阅

maxVisibleItems()

PySide2.QtWidgets.QComboBox. setMinimumContentsLength ( characters )
参数

characters int

PySide2.QtWidgets.QComboBox. setModel ( model )
参数

model QAbstractItemModel

把模型设置为 model . model 必须不是 None 。若希望清零模型的内容,调用 clear() .

另请参阅

model() clear()

PySide2.QtWidgets.QComboBox. setModelColumn ( visibleColumn )
参数

visibleColumn int

另请参阅

modelColumn()

PySide2.QtWidgets.QComboBox. setPlaceholderText ( placeholderText )
参数

placeholderText – unicode

另请参阅

placeholderText()

PySide2.QtWidgets.QComboBox. setRootModelIndex ( index )
参数

index QModelIndex

设置根模型项 index 为组合框项。

另请参阅

rootModelIndex()

PySide2.QtWidgets.QComboBox. setSizeAdjustPolicy ( policy )
参数

policy SizeAdjustPolicy

PySide2.QtWidgets.QComboBox. setValidator ( v )
参数

v QValidator

设置 validator 以代替当前使用的验证器。

注意

验证器被移除,当 editable 特性变为 false .

另请参阅

validator()

PySide2.QtWidgets.QComboBox. setView ( itemView )
参数

itemView QAbstractItemView

将组合框弹出所用视图设为给定 itemView 。组合框拥有视图的所有权。

注意:若想要使用方便视图 (像 QListWidget , QTableWidget or QTreeWidget ),确保调用 setModel() on the combobox with the convenience widgets model before calling this function.

另请参阅

view()

PySide2.QtWidgets.QComboBox. showPopup ( )

显示组合框项列表。若列表为空,就不会展示项。

若重新实现此函数以展示自定义弹出,确保调用 hidePopup() to reset the internal state.

另请参阅

hidePopup()

PySide2.QtWidgets.QComboBox. sizeAdjustPolicy ( )
返回类型

SizeAdjustPolicy

PySide2.QtWidgets.QComboBox. textActivated ( arg__1 )
参数

arg__1 – unicode

PySide2.QtWidgets.QComboBox. textHighlighted ( arg__1 )
参数

arg__1 – unicode

PySide2.QtWidgets.QComboBox. validator ( )
返回类型

QValidator

返回用于约束组合框文本输入的验证器。

另请参阅

setValidator() editable

PySide2.QtWidgets.QComboBox. view ( )
返回类型

QAbstractItemView

返回用于组合框弹出的列表视图。

另请参阅

setView()