QComboBox作为下拉列表框,也是常用的控件之一,最常见的就是win10文件浏览器上的地址输入栏、QQ登录框等。本篇文章将从以下几个方面介绍QComboBox:
[1]添加纯文本item
[2]添加带图标的item
[3]信号
[4]item携带自定义数据
[5]编辑
[1]添加纯文本item
QComboBox添加纯文本列表项可以单个插入(以QString方式)、多个插入(以QStringList方式)、单个指定位置插入、多个指定位置插入,代码如下:
//添加单个item
ui->comboBox->addItem("测试项目1");
//批量添加item
ui->comboBox->addItems({"测试项目2","测试项目3","测试项目4"});
//指定位置插入单个item(在最后插入)
ui->comboBox->insertItem(ui->comboBox->count(),"测试项目5");
//指定位置插入多个item(最前面插入)
ui->comboBox->insertItems(0,{"测试项目-1","测试项目0"});
程序运行结果:
[2]添加带图标的item
通过调用以下接口可以实现添加带图标的item:
void addItem(const QIcon &icon, const QString &text, const QVariant &userData = QVariant())
void insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userData = QVariant())
也可以调用以下接口修改或设置已有item的图标:
void setItemIcon(int index, const QIcon &icon)
代码如下:
//设置图标大小为32*32(默认为16*16)
ui->comboBox->setIconSize(QSize(32,32));
ui->comboBox->addItem(QIcon(":/new/prefix1/Res/folder.png"),"测试项目6");
ui->comboBox->insertItem(ui->comboBox->count(),QIcon(":/new/prefix1/Res/folder.png"),"测试项目7");
ui->comboBox->setItemIcon(1,QIcon(":/new/prefix1/Res/folder.png"));
程序运行结果:
[3]信号
QComboBox有下面这几个信号事件:
void activated(int index)
void activated(const QString &text)
void currentIndexChanged(int index)
void currentIndexChanged(const QString &text)
void currentTextChanged(const QString &text)
void editTextChanged(const QString &text)
void highlighted(int index)
void highlighted(const QString &text)
当有列表项被点击时,会发出activated信号,传递index或text参数给信号所对应的槽函数。
当选择项发生变化时,会发出currentIndexChanged信号,传递index或text参数给信号所对应的槽函数。
当列表文本改变时(需要打开可编辑属性),则会触发editTextChanged信号,传递改变后的文本给信号所对应的槽函数。
当某个列表项高亮时(如将鼠标放在列表项上面),会发出highlighted信号,传递index或text参数给信号所对应的槽函数。
下面以void activated(int index)信号作为示例:
信号与槽绑定
connect(ui->comboBox,SIGNAL(activated(int)),this,SLOT(Activated(int)));
槽函数的实现:
void Widget::Activated(int index)
{
qDebug() << "Activated:" << ui->comboBox->itemText(index);
}
[4]item携带自定义数据
QComboBox可以在添加或插入item时指定自定义数据,也可以根据下标对已有的item进行自定义数据插入,代码如下:
自定义数据:
struct Student
{
QString name;
int age;
};
//声明自定义类型,使得Qt能找到此类型的定义
Q_DECLARE_METATYPE(Student);
addItem时直接指定自定义数据:
QVariant useVar;
Student s1 = {"小明",20};
useVar.setValue(s1);
ui->comboBox->addItem("小明",useVar);
在Activated槽函数中添加以下代码:
QVariant useVar = ui->comboBox->itemData(index);
//判断先前是否有指定数据
if(useVar == QVariant::Invalid)
{
qDebug() << "no data";
return;
}
qDebug() << useVar.value<Student>().name << ":" << useVar.value<Student>().age;
运行结果:
[5]编辑
QComboBox可以通过打开可编辑属性,实现与QLineEdit相类似的部分功能。
//打开可编辑
ui->comboBox->setEditable(true);
在打开可编辑属性后,同样可以通过setValidator接口限定其输入格式,通过setCompleter接口设置输入自动补全。如果对这两个接口不熟悉的话可以点击这里查看,在这里就不重复说明了。
在这里需要说明的是,默认情况下,QComboBox的编辑模式并不会删除或改变已有的item,而是在用户按下回车后,在最顶端添加刚刚编辑的内容。如果想要改变默认属性,可以通过setInsertPolicy接口进行修改。
关于setInsertPolicy的参数列表的解释,在这里直接贴上官方帮助文档里的内容:
QComboBox::NoInsert The string will not be inserted into the combobox.
QComboBox::InsertAtTop The string will be inserted as the first item in the combobox.
QComboBox::InsertAtCurrent The current item will be replaced by the string.
QComboBox::InsertAtBottom The string will be inserted after the last item in the combobox.
QComboBox::InsertAfterCurrent The string is inserted after the current item in the combobox.
QComboBox::InsertBeforeCurrent The string is inserted before the current item in the combobox.
QComboBox::InsertAlphabetically The string is inserted in the alphabetic order in the combobox.