下拉框comobox--可勾选多个表项check

ComboBoxWidget::ComboBoxWidget()
{
    ui->setupUi(this);

    //安装过滤器
    ui->comboBox->installEventFilter(this);

    //下拉列表

    m_listWidget = new QListWidget;
    ui->comboBox->setModel(m_listWidget->model());
    ui->comboBox->setView(m_listWidget);
    ui->comboBox->setEditable(true);
    ui->comboBox->lineEdit()->setReadOnly(true);

    
    connect(ui->comboBox, SIGNAL(currentTextChanged(const QString &)), this, SLOT(slot_curTextChanged(const QString &)));
    connect(m_listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slot_itemClicked(QListWidgetItem *)));
}

//禁用鼠标点击,滚轮事件

bool ComboBoxWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->comboBox)
    {
        if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick)
        {
            if (ui->comboBox == NULL || ui->comboBox->isVisible() == false)
            {
                return true;
            }
        }
        else if (event->type() == QEvent::Wheel)
        {
            return true;
        }
    }
    return QWidget::eventFilter(watched, event);
}

//下拉列表添加表项

void ComboBoxWidget::addItem(const QString& text)
{
    QListWidgetItem *item = new QListWidgetItem;
    item->setFlags(Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable);
    item->setCheckState(Qt::Unchecked);

    item->setText(text);
    m_listWidget->addItem(item);
}

//点击表项,自动勾选

void ComboBoxWidget::slot_itemClicked(QListWidgetItem *item)
{
    ui->comboBox->blockSignals(true);
    Qt::CheckState state = item->checkState();
    if (state == Qt::Checked)
    {
        state = Qt::Unchecked;
    }
    else
    {
        state = Qt::Checked;
    }
    item->setCheckState(state);
    ui->comboBox->blockSignals(false);

//更新当前text

//……
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值