当前位置: 首页 > news >正文

【Qt】QTableView添加下拉框过滤条件

实现通过带复选框的下拉框来为表格添加过滤条件
在这里插入图片描述

带复选框的下拉框

.h文件

#pragma once
#include <QCheckBox>
#include <QComboBox>
#include <QEvent>
#include <QLineEdit>
#include <QListWidget>class TableComboBox : public QComboBox
{Q_OBJECTpublic:TableComboBox(QWidget* parent = NULL);~TableComboBox();// 隐藏下拉框virtual void hidePopup();// 添加一条选项void addItem(const QString& _text, const QVariant& _variant = QVariant());// 添加多条选项void addItems(const QStringList& _text_list);// 返回当前选中选项QStringList currentText();// 返回当前选项条数int count() const;// 设置搜索框默认文字void SetSearchBarPlaceHolderText(const QString _text);// 设置文本框默认文字void SetPlaceHolderText(const QString& _text);// 下拉框状态恢复默认void ResetSelection();// 清空所有内容void clear();// 文本框内容清空void TextClear();// 设置选中文本--单void setCurrentText(const QString& _text);// 设置选中文本--多void setCurrentText(const QStringList& _text_list);// 设置搜索框是否禁用void SetSearchBarHidden(bool _flag);protected:// 事件过滤器virtual bool eventFilter(QObject* watched, QEvent* event);// 滚轮事件virtual void wheelEvent(QWheelEvent* event);// 按键事件virtual void keyPressEvent(QKeyEvent* event);private slots:// 文本框文本变化void stateChange(int _row);// 点击下拉框选项void itemClicked(int _index);signals:// 发送当前选中选项void selectionChange(const QString _data);private:// 下拉框QListWidget* pListWidget;// 文本框,搜索框QLineEdit *pLineEdit, *pSearchBarEdit;// 搜索框显示标志bool isHidden;// 下拉框显示标志bool isShow;
};

.cpp文件

#include "PowerTableComboBox.h"#include <QMessageBox>#define FREEPTR(p) \if (p != NULL) \{              \delete p;  \p = NULL;  \}TableComboBox::TableComboBox(QWidget* parent): QComboBox(parent), isHidden(true), isShow(false)
{pListWidget = new QListWidget();pLineEdit = new QLineEdit();pSearchBarEdit = new QLineEdit();QListWidgetItem* currentItem = new QListWidgetItem(pListWidget);pSearchBarEdit->setPlaceholderText("搜索...");pSearchBarEdit->setClearButtonEnabled(true);pListWidget->addItem(currentItem);pListWidget->setItemWidget(currentItem, pSearchBarEdit);pLineEdit->setReadOnly(true);pLineEdit->installEventFilter(this);pLineEdit->setStyleSheet("QLineEdit:disabled{background:rgb(233,233,233);}");this->setModel(pListWidget->model());this->setView(pListWidget);this->setLineEdit(pLineEdit);connect(this, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &TableComboBox::itemClicked);
}TableComboBox::~TableComboBox()
{FREEPTR(pLineEdit);FREEPTR(pSearchBarEdit);
}void TableComboBox::hidePopup()
{isShow = 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 TableComboBox::addItem(const QString& _text, const QVariant& _variant)
{Q_UNUSED(_variant);QListWidgetItem* item = new QListWidgetItem(pListWidget);QCheckBox* checkbox = new QCheckBox(this);checkbox->setText(_text);pListWidget->setItemWidget(item, checkbox);pListWidget->addItem(item);connect(checkbox, &QCheckBox::stateChanged, this, &TableComboBox::stateChange);checkbox->setChecked(true);
}void TableComboBox::addItems(const QStringList& _text_list)
{for (const auto& text_one : _text_list){addItem(text_one);}
}QStringList TableComboBox::currentText()
{QStringList text_list;if (!pLineEdit->text().isEmpty()){// 以空格为分隔符分割字符串text_list = pLineEdit->text().split(' ');}return text_list;
}int TableComboBox::count() const
{int count = pListWidget->count() - 1;if (count < 0){count = 0;}return count;
}void TableComboBox::SetSearchBarPlaceHolderText(const QString _text)
{pSearchBarEdit->setPlaceholderText(_text);
}void TableComboBox::SetPlaceHolderText(const QString& _text)
{pLineEdit->setPlaceholderText(_text);
}void TableComboBox::ResetSelection()
{int count = pListWidget->count();for (int i = 1; i < count; i++){QWidget* widget = pListWidget->itemWidget(pListWidget->item(i));QCheckBox* check_box = static_cast<QCheckBox*>(widget);check_box->setChecked(false);}
}void TableComboBox::clear()
{pLineEdit->clear();pListWidget->clear();QListWidgetItem* currentItem = new QListWidgetItem(pListWidget);pSearchBarEdit->setPlaceholderText("搜索...");pSearchBarEdit->setClearButtonEnabled(true);pListWidget->addItem(currentItem);pListWidget->setItemWidget(currentItem, pSearchBarEdit);SetSearchBarHidden(isHidden);
}void TableComboBox::TextClear()
{pLineEdit->clear();ResetSelection();
}void TableComboBox::setCurrentText(const QString& _text)
{int count = pListWidget->count();for (int i = 1; i < count; i++){QWidget* widget = pListWidget->itemWidget(pListWidget->item(i));QCheckBox* check_box = static_cast<QCheckBox*>(widget);if (_text.compare(check_box->text()))check_box->setChecked(true);}
}void TableComboBox::setCurrentText(const QStringList& _text_list)
{int count = pListWidget->count();for (int i = 1; i < count; i++){QWidget* widget = pListWidget->itemWidget(pListWidget->item(i));QCheckBox* check_box = static_cast<QCheckBox*>(widget);if (_text_list.contains(check_box->text()))check_box->setChecked(true);}
}void TableComboBox::SetSearchBarHidden(bool _flag)
{isHidden = _flag;pListWidget->item(0)->setHidden(isHidden);
}bool TableComboBox::eventFilter(QObject* watched, QEvent* event)
{if (watched == pLineEdit && event->type() == QEvent::MouseButtonRelease && this->isEnabled()){showPopup();return true;}return false;
}void TableComboBox::wheelEvent(QWheelEvent* event)
{Q_UNUSED(event);
}void TableComboBox::keyPressEvent(QKeyEvent* event)
{QComboBox::keyPressEvent(event);
}void TableComboBox::stateChange(int _row)
{Q_UNUSED(_row);QString selected_data("");int count = pListWidget->count();for (int i = 1; i < count; i++){QWidget* widget = pListWidget->itemWidget(pListWidget->item(i));QCheckBox* check_box = static_cast<QCheckBox*>(widget);if (check_box->isChecked()){// 添加空格做为分割符selected_data.append(check_box->text()).append(" ");}}selected_data.chop(1);if (!selected_data.isEmpty()){pLineEdit->setText(selected_data);}else{pLineEdit->clear();}// 文字从最左边开始显示pLineEdit->setToolTip(selected_data);pLineEdit->setSelection(0, 0);pLineEdit->setCursorPosition(0);emit selectionChange(selected_data);
}void TableComboBox::itemClicked(int _index)
{if (_index != 0){QCheckBox* check_box = static_cast<QCheckBox*>(pListWidget->itemWidget(pListWidget->item(_index)));check_box->setChecked(!check_box->isChecked());}
}

表格过滤器代理类

class TableProxyModel : public QSortFilterProxyModel
{
public:TableProxyModel (QObject* parent = nullptr): QSortFilterProxyModel(parent){}protected:bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override{QModelIndex targetTypeIndex = sourceModel()->index(source_row, 0, source_parent);QModelIndex boomNameIndex = sourceModel()->index(source_row, 1, source_parent);QModelIndex destructionDegreeIndex = sourceModel()->index(source_row, 2, source_parent);QString typeText = sourceModel()->data(targetTypeIndex).toString();QString nameText = sourceModel()->data(boomNameIndex).toString();int levelText = sourceModel()->data(destructionDegreeIndex).toInt();if (!isFilter){return true;}bool matchType = false;bool matchName = false;bool levelText = false;for (QString type : sType){if (typeText == type){matchType = true;break;}}for (QString name : sName){if (nameText == name){matchName = true;break;}}for (int level: nLevelVec){if (levelText == level){matchLevel = true;break;}}if (matchLevel && matchName && matchType){return true;}return false;}public:QStringList sType;QStringList sName;QVector<int> nLevelVec;bool isFilter = false;
};

表格设置代理关联下拉框内容变更

添加代理

    tableView = new QTableView;proxyModel = new TableProxyModel;// 设置过滤规则来执行过滤proxyModel->setFilterRole(Qt::DisplayRole);proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);proxyModel->setSourceModel(model);tableView->setModel(proxyModel);

下拉框关联表格代理

  1. 通过信号与槽关联
 connect(typeBox, &TableComboBox::selectionChange, this, &ShowTableView::filterBtnClick);
  1. 获取下拉框内容传到代理类做过滤
void ShowTableView::filterBtnClick(QString)
{// 复位proxyModel->isFilter = true;proxyModel->sType.clear();proxyModel->sName.clear();proxyModel->nLevelVec.clear();// 获取筛选条件proxyModel->sType = typeBox->currentText();proxyModel->sName= boomNameBox->currentText();for (QString level: levelBox->currentText()){proxyModel->nLevelVec.push_back(level.toInt());}proxyModel->setFilterFixedString("");
}
  1. 取消过滤,复位
void ShowTableView::noFilterBtnClick()
{proxyModel->isFilter = false;proxyModel->setFilterFixedString("");
}

截图

  1. 过滤前
  2. 过滤后
    在这里插入图片描述
http://www.lryc.cn/news/470986.html

相关文章:

  • 部署DNS主从服务器
  • 从可逆计算看低代码
  • 设计模式最佳实践代码总结 - 结构型设计模式篇 - 侨接设计模式最佳实践
  • 【软件测试】python——Unittest
  • Maven:详解 clean 和 install 命令的使用
  • HTTP与RPC
  • 解决蓝牙键盘按键错乱的问题
  • MiL.k X Biggie 奇妙宇宙来袭!
  • 云服务器中删除非空目录(包含文件和子目录)rm -rf <directory_name>
  • 1991-2024年经管类国自然、国社科立项名单(附68份国自然标书)-最新出炉 附下载链接
  • Flutter问题记录 - 布局中莫名其妙的白线/缝隙
  • 从零学习大模型(七)-----LoRA(中)
  • Java知识巩固(十二)
  • 一家光伏企业终止,恐不具行业代表性,市占率仅为2.35%
  • 企业计算机监控软件是什么?6款电脑监控软件分享!提升企业管理效率,吐血推荐!
  • VisionPro —— CogOCRMaxTool工具详解
  • 网站安全问题都有哪些,分别详细说明
  • DiskGenius一键修复磁盘损坏
  • Matlab实现鼠群优化算法优化回声状态网络模型 (ROS-ESN)(附源码)
  • nfs作业
  • Linux 基础io_理解文件系统_软硬链接_动静态库
  • 大语言模型参数传递、model 构建与tokenizer构建(基于llama3模型)
  • 使用 `screen` + `nohup` 实现高效日志记录和多环境任务管理
  • 【探索数字孪生,引领未来技术】
  • Tcp_Sever(线程池版本的 TCP 服务器)
  • 第十一章 Vue生命周期及生命周期的四个阶段
  • 展厅展会客流显示屏的客流统计功能如何实现
  • golang正则表达式的使用及举例
  • Flutter杂学: iOS 上启用自动填充和关联域
  • 接口自动化-框架搭建(Python+request+pytest+allure)