(&QComboBox::activated), this, &MultiSelectComboBox::itemClicked);}MultiSelectComboBox::~MultiSelectComboBox(){}void MultiSelectComboBox::hidePopup(){show_flag_ = false;int width = this->width();int height = this->height();int x = QCursor::pos().x() - mapToGlobal(geometry().topLeft()).x() + geometry().x();int y = QCursor::pos().y() - mapToGlobal(geometry().topLeft()).y() + geometry().y();if (x >= 0 && x <= width && y >= this->height() && y <= height + this->height()){}else{QComboBox::hidePopup();}}void MultiSelectComboBox::addItem(const QString& _text, const QVariant& _variant /*= QVariant()*/){Q_UNUSED(_variant);QListWidgetItem* item = new QListWidgetItem(list_widget_);QCheckBox* checkbox = new QCheckBox(this);checkbox->setText(_text);list_widget_->addItem(item);list_widget_->setItemWidget(item, checkbox);connect(checkbox, &QCheckBox::stateChanged, this, &MultiSelectComboBox::stateChange);}void MultiSelectComboBox::addItems(const QStringList& _text_list){for (const auto& text_one : _text_list){addItem(text_one);}}QStringList MultiSelectComboBox::currentText(){QStringList text_list;if (!line_edit_->text().isEmpty()){//以;为分隔符分割字符串text_list = line_edit_->text().split(':');}return text_list;}int MultiSelectComboBox::count() const{int count = list_widget_->count() - 1;if (count < 0){count = 0;}return count;}void MultiSelectComboBox::SetSearchBarPlaceHolderText(const QString _text){search_bar_->setPlaceholderText(_text);}void MultiSelectComboBox::SetPlaceHolderText(const QString& _text){line_edit_->setPlaceholderText(_text);}void MultiSelectComboBox::ResetSelection(){int count = list_widget_->count();for (int i = 1; i < count; i++){//获取对应位置的QWidget对象QWidget *widget = list_widget_->itemWidget(list_widget_->item(i));//将QWidget对象转换成对应的类型QCheckBox *check_box = static_cast(widget);check_box->setChecked(false);}}void MultiSelectComboBox::clear(){line_edit_->clear();list_widget_->clear();QListWidgetItem* currentItem = new QListWidgetItem(list_widget_);search_bar_->setPlaceholderText("Search.........");search_bar_->setClearButtonEnabled(true);list_widget_->addItem(currentItem);list_widget_->setItemWidget(currentItem, search_bar_);SetSearchBarHidden(hidden_flag_);connect(search_bar_, SIGNAL(textChanged(const QString&)), this, SLOT(onSearch(const QString&)));}void MultiSelectComboBox::TextClear(){line_edit_->clear();ResetSelection();}void MultiSelectComboBox::setCurrentText(const QString& _text){int count = list_widget_->count();for (int i = 1; i < count; i++){//获取对应位置的QWidget对象QWidget *widget = list_widget_->itemWidget(list_widget_->item(i));//将QWidget对象转换成对应的类型QCheckBox *check_box = static_cast(widget);if (_text.compare(check_box->text()))check_box->setChecked(true);}}void MultiSelectComboBox::setCurrentText(const QStringList& _text_list){int count = list_widget_->count();for (int i = 1; i < count; i++){//获取对应位置的QWidget对象QWidget *widget = list_widget_->itemWidget(list_widget_->item(i));//将QWidget对象转换成对应的类型QCheckBox *check_box = static_cast(widget);if (_text_list.contains(check_box->text()))check_box->setChecked(true); }}void MultiSelectComboBox::SetSearchBarHidden(bool _flag){hidden_flag_ = _flag;list_widget_->item(0)->setHidden(hidden_flag_);}bool MultiSelectComboBox::eventFilter(QObject *watched, QEvent *event){//设置点击输入框也可以弹出下拉框if (watched == line_edit_ && event->type() == QEvent::MouseButtonRelease && this->isEnabled()){showPopup();return true;}return false;}void MultiSelectComboBox::wheelEvent(QWheelEvent *event){//禁用QComboBox默认的滚轮事件Q_UNUSED(event);}void MultiSelectComboBox::keyPressEvent(QKeyEvent *event){QComboBox::keyPressEvent(event);}void MultiSelectComboBox::stateChange(int _row){Q_UNUSED(_row);QString selected_data("");int count = list_widget_->count();for (int i = 1; i < count; i++){QWidget *widget = list_widget_->itemWidget(list_widget_->item(i));QCheckBox *check_box = static_cast