目录
- 前言
- 1、 功能描述
- 2、代码实现
- 总结
前言
本文记录了一种通过继承 QComboBox 实现下拉列表多选功能的方法。效果如下图所示:
1、 功能描述
普通的下拉列表只支持选择一个选项,在软件开发过程中,经常会遇到下拉列表支持选择多个选项的需求,下面的代码实现了下拉列表多选功能,支持设置弹窗的高度,支持添加显示文本和用户数据,支持设置默认选中项目,支持获取选中数据。
2、代码实现
1、头文件
#ifndef LLCOMBOBOX_H
#define LLCOMBOBOX_H#include <QComboBox>class QListWidgetItem;
class LLComboBoxPrivate;
class LLComboBox : public QComboBox
{Q_OBJECT
public:explicit LLComboBox(QWidget* parent = NULL);/*** @brief addDataItem 添加数据* @param text* @param userData*/void addDataItem(const QString &text, const QVariant &userData = QVariant());/*** @brief setSelectedData 设置选中的数据* @param selectedData*/void setSelectedData(const QStringList &selectedData);/*** @brief setPopupViewHeight 设置下拉列表弹窗的高度,默认值100* @param height*/void setPopupViewHeight(int height);/*** @brief selectedDataText 获取选择的数据* @return*/QStringList selectedDataText();/*** @brief selectedUserData 获取选择数据对应的用户数据* @return*/QList<QVariant> selectedUserData();private slots:void slot_itemChanged(QListWidgetItem *item);
private:LLComboBoxPrivate *m_pd;
};#endif // LLCOMBOBOX_H
2、cpp文件
#include <QListWidget>
#include <QCheckBox>
#include <QList>
#include <QLineEdit>
#include <QDebug>#include "llcombobox.h"class LLComboBoxPrivate
{
public:QStringList selectedDataList;QListWidget *pListWidget;
};LLComboBox::LLComboBox(QWidget *parent): QComboBox(parent), m_pd(new LLComboBoxPrivate)
{m_pd->pListWidget = new QListWidget;m_pd->pListWidget->setFixedHeight(100);connect(m_pd->pListWidget, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(slot_itemChanged(QListWidgetItem *)));setView(m_pd->pListWidget);//设置Editable为true,lineEdit() 函数才不返回空setEditable(true);
}void LLComboBox::addDataItem(const QString &text, const QVariant &userData)
{m_pd->pListWidget->blockSignals(true);QListWidgetItem *pItem = new QListWidgetItem(text, m_pd->pListWidget);pItem->setData(Qt::UserRole, userData);pItem->setCheckState(Qt::Unchecked);//设置QListWidgetItem 可交互且可以选中和取消选中pItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);m_pd->pListWidget->addItem(pItem);QComboBox::addItem(text);QComboBox::setCurrentText("");m_pd->pListWidget->blockSignals(false);
}void LLComboBox::setSelectedData(const QStringList &selectedData)
{m_pd->selectedDataList.clear();m_pd->selectedDataList += selectedData;QString text = "";for (int var = 0; var < m_pd->selectedDataList.size(); ++var){m_pd->pListWidget->blockSignals(true);QListWidgetItem *pItem = m_pd->pListWidget->item(var);pItem->setCheckState(Qt::Checked);m_pd->pListWidget->blockSignals(false);text.append(m_pd->selectedDataList[var]);if(var < m_pd->selectedDataList.size() - 1){text.append(",");}}lineEdit()->setText(text);
}QStringList LLComboBox::selectedDataText()
{return m_pd->selectedDataList;
}QList<QVariant> LLComboBox::selectedUserData()
{QList<QVariant> dataList;for (int var = 0; var < m_pd->selectedDataList.size(); ++var){QListWidgetItem *pItem = m_pd->pListWidget->item(var);if(pItem->checkState() == Qt::Checked){dataList.append(pItem->data(Qt::UserRole));}}return dataList;
}void LLComboBox::slot_itemChanged(QListWidgetItem *item)
{qDebug() << QString("text=") << item->text();if(m_pd->selectedDataList.contains(item->text())){m_pd->selectedDataList.removeOne(item->text());}else{m_pd->selectedDataList.append(item->text());}QString text = "";for (int var = 0; var < m_pd->selectedDataList.size(); ++var){text.append(m_pd->selectedDataList[var]);if(var < m_pd->selectedDataList.size() - 1){text.append(",");}}lineEdit()->setText(text);
}
总结
以上就是本文的所有内容了,欢迎留言讨论,源码下载地址 多选下拉列表下载地址