ComboBox控件
1.控件位置
Input Widgets→ComboBox
2.控件介绍
ComboBox控件(组合框)的样式如图3-63所示。Qt Creator的ComboBox和Qt Designer的ComboBox样式有些不同。
![]() |
图3-63 ComboBox控件 |
3.控件设置选项
在ComboBox控件的properties选项中,一般常对以下选项进行设置。
name:该控件对应源代码中的名称;
font:设置显示的字体;
editable:用来获取或设置一个值,以确定ComboBox的编辑框是否可编辑。值为true时为可编辑,值为false时为只读;
currentIndex:当前选项的索引;
maxVisibleItems:ComboBox可见的最大项目数;
maxCount:ComboBox的最大项目数。
4.常用成员函数
构造一个名称为name、父对象为parent的ComboBox。
- 1) QComboBox::QComboBox ( QWidget *parent = 0, const char *name = 0 )
构造一个名称为name、父对象为parent的ComboBox。如果rw是true,则编辑栏可编辑;否则只能选择ComboBox项目之一。
- 2) QComboBox::QComboBox ( bool rw, QWidget *parent = 0, const char *name = 0 )
删除ComboBox中的所有项目。
- 3) void QComboBox::clear () [slot]
返回ComboBox中的项目数。
- 4) int QComboBox::count () const
返回ComboBox中当前项目的索引。
- 5) int QComboBox::currentItem () const
返回组合框的当前项目文本。
- 6) QString QComboBox::currentText () const
在索引index处,插入一个文本为t的项目。如果index是负数,该项目将被追加到末尾。
- 7) void QComboBox::insertItem ( const QString & t, int index = -1 )
在索引index处,插入一个图标为pixmap的项目。如果index是负数,该项目将被追加到末尾。
- 8) void QComboBox::insertItem ( const QPixmap & pixmap, int index = -1 )
在索引index处,插入一个文本为t和图标为pixmap的项目。如果index是负数,该项目将被追加到末尾。
- 9) void QComboBox::insertItem ( const QPixmap & pixmap, const QString & text, int index = -1 )
返回组合框的当前项目文本。
- 10) QString QComboBox::currentText () const
删除索引index处项目。
- 11) void QComboBox::removeItem ( int index )
把索引index处的项目设为当前项目。
- 12) void QComboBox::setCurrentItem ( int index ) [virtual]