QComboBox多选功能实现类

//头文件
#ifndef MULISELCOMBOX_H
#define MULISELCOMBOX_H
#include 
#include 

class QListWidget;
class QLineEdit;

class MuliSelCombox : public QComboBox
{
    Q_OBJECT
public:
    explicit MuliSelCombox(QWidget *parent = nullptr);

    //重新设置sql语句,同时刷新
    void setSql(const QString& sSql);

    //获取选择的项的标签名
    QStringList getSelectTagNames();

    //重写下拉框隐藏函数
    void hidePopup();

private:
    //刷新
    void refresh();

signals:
    //下拉框收起信号
    void sigHidePopup();

private slots:
    void onStateChanged(int);

private:
    QString m_sSql;
    QListWidget *m_pListWidget;  //下拉框列表
    QLineEdit *m_pLineEdit;      //用于显示选择的项
    QStringList m_selectTagName; //选择的标签名
};

#endif // MULISELCOMBOX_H
//实现文件
#include "muliselcombox.h"
#include 
#include "common_global.h"
#include 
#include "kbdtabledatamgr.h"
#include 
#include 
#include 

MuliSelCombox::MuliSelCombox(QWidget *parent):QComboBox(parent)
{
    m_pLineEdit = NULL;
}

void MuliSelCombox::setSql(const QString &sSql)
{
    m_sSql = sSql;
    refresh();
}

void MuliSelCombox::refresh()
{
    clear();
    delete model();
    delete view();
    QSqlQuery result;
    if(!KbdTableDataMgr::getInstance()->selectOnly(m_sSql,result))
        return;
    m_pListWidget = new QListWidget(this);
    if(m_pLineEdit == NULL)
    {
        m_pLineEdit = new QLineEdit(this);
        m_pLineEdit->setReadOnly(true);
        setLineEdit(m_pLineEdit);
    }
    while(result.next()){
        QString tagName = result.value(CN_TAG_NAME).toString();
        QString desc = result.value(CN_DESC).toString();
        QListWidgetItem *pItem = new QListWidgetItem(m_pListWidget);
        pItem->setData(Qt::UserRole, tagName);
        QCheckBox *pCheckBox = new QCheckBox(this);
        pCheckBox->setText(desc);
        m_pListWidget->addItem(pItem);
        m_pListWidget->setItemWidget(pItem, pCheckBox);
        connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    }
    setModel(m_pListWidget->model());
    setView(m_pListWidget);
}

QStringList MuliSelCombox::getSelectTagNames()
{
    return m_selectTagName;
}

void MuliSelCombox::hidePopup()
{
    emit sigHidePopup();
    QComboBox::hidePopup();
}

void MuliSelCombox::onStateChanged(int state)
{
    //实现设备组多选状态变化
    Q_UNUSED(state);
    m_selectTagName.clear();
    QString selectdData("");
    int nCount = m_pListWidget->count();
    for(int i = 0; i < nCount; ++i)
    {
        QListWidgetItem *pItem = m_pListWidget->item(i);
        QWidget *pWidget = m_pListWidget->itemWidget(pItem);
        QCheckBox *pCheckBox = (QCheckBox *)pWidget;
        if(pCheckBox->isChecked())
        {
            QString strText = pCheckBox->text();
            selectdData.append(strText).append(";");
            QString strData = pItem->data(Qt::UserRole).toString();
            m_selectTagName.append(strData);
        }
    }
    if(selectdData.endsWith(";"))
        selectdData.remove(selectdData.count() - 1, 1);
    if(!selectdData.isEmpty())
    {
        m_pLineEdit->setText(selectdData);
        m_pLineEdit->setToolTip(selectdData);
    }
    else
    {
        m_pLineEdit->clear();
    }
}

 

你可能感兴趣的:(c++)