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

Qt表格入门(优化篇)


摘要:
为提升大数据量下的渲染性能,本文通过 QStyledItemDelegate 直接绘制单选按钮(使用 QStyleOptionButtondrawControl),并在 editorEvent 中处理点击逻辑,避免创建真实控件,显著优化了加载与显示效率。

关键词:
QStyledItemDelegateQStyleOptionButtondrawControleditorEvent、优化、渲染

版本:Qt5.14.2

问题描述:

    在上一篇文章中,使用样式代理类QStyledItemDelegate和基础组件(QRadioButtonQWidget)的组合比较简易地实现了功能,但是在后续使用中发现,大量数据导致视图渲染卡顿,需要对代码进一步优化。

代码如下:

// 优化代理类
class RadioDelegateV2 : public QStyledItemDelegate
{Q_OBJECT
public:RadioDelegateV2(QObject *parent = nullptr);~RadioDelegateV2();protected:void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
};RadioDelegateV2::RadioDelegateV2(QObject *parent) : QStyledItemDelegate(parent)
{}RadioDelegateV2::~RadioDelegateV2()
{}void RadioDelegateV2::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{int state = index.data(Qt::DisplayRole).toInt(); // 0=女, 1=男// 分割矩形为左右两半QRect rect = option.rect;QRect leftRect(rect.left(), rect.top(), rect.width() / 2, rect.height());QRect rightRect(rect.left() + rect.width() / 2, rect.top(), rect.width() / 2, rect.height());// 绘制“男”按钮(左)QStyleOptionButton btnOn;btnOn.rect = leftRect;btnOn.text = "男";btnOn.state |= QStyle::State_Enabled;if (state == 1) {btnOn.state |= QStyle::State_Sunken | QStyle::State_On;} else {btnOn.state |= QStyle::State_Raised;}// 绘制“女”按钮(右)QStyleOptionButton btnOff;btnOff.rect = rightRect;btnOff.text = "女";btnOff.state |= QStyle::State_Enabled;if (state == 0) {btnOff.state |= QStyle::State_Sunken | QStyle::State_On;} else {btnOff.state |= QStyle::State_Raised;}QApplication::style()->drawControl(QStyle::CE_RadioButton, &btnOn,  painter);QApplication::style()->drawControl(QStyle::CE_RadioButton, &btnOff, painter);
}QSize RadioDelegateV2::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{Q_UNUSED(index);// 返回足够宽以容纳两个按钮QFontMetrics fm(option.font);int width = fm.horizontalAdvance("男") + fm.horizontalAdvance("女") + 40; // 加 paddingint height = qMax(fm.height() + 8, 24);return QSize(width, height);
}bool RadioDelegateV2::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{if (event->type() == QEvent::MouseButtonPress) {QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);QPoint pos = mouseEvent->pos();QRect rect = option.rect;int half = rect.width() / 2;int newState;if (pos.x() < rect.left() + half) {newState = 1; // 点击左侧 → 男} else {newState = 0; // 点击右侧 → 女}// 更新模型model->setData(index, newState, Qt::DisplayRole);return true;}return false;
}

代码说明:

  1. paint()函数中,创建两个QStyleOptionButton并设置属性;两个按钮根据数据(性别)设置不同的状态;然后使用drawControl()函数绘制按钮;
  2. sizeHint()函数中,计算可以容纳下两个按钮的宽度;
  3. editorEvent()函数中,对编辑事件进行操作,判断点击区域,然后更新数据。

其他说明:

  1. openPersistentEditor()函数调用可以全部优化掉 ;
  2. 如果数据变化有连接的信号,可以使用QStandardItemModel void itemChanged(QStandardItem *item)信号修改。

参考文献:

  • QTableView 一列添加两个按钮

http://www.jsqmd.com/news/514160/

相关文章:

  • HuggingFace模型下载路径修改指南:告别~/.cache/huggingface爆盘困扰
  • 车载测试的学习笔记
  • 华为设备实战:3种代理ARP配置全解析(路由式+VLAN内+VLAN间)
  • 2026年驻马店GEO优化公司深度测评:从技术适配到效果落地的选型指南 - 小白条111
  • [向量数据库/AI] Milvus Java SDK 使用指南
  • Qwen3.5-9B效果展示:同一张图多角度提问,视觉推理能力对比实录
  • 嵌入式C语言代码优化实战:从编译器到硬件的性能调优
  • 用Python和GNU Radio玩转USRP:从环境搭建到第一个FM收音机实战
  • 2026年招商加盟GEO企业培训深度分析:从需求适配到效果验证的避坑指南 - 小白条111
  • [向量数据库] Milvus Java SDK 使用指南
  • 从0开始学python -- 数据类型和变量
  • 漂亮大气的酒店和旅游业务预订网站模板WordPress主题
  • InstructPix2Pix企业实操:电商运营人员日均处理200+商品图方案
  • 腾讯后端面试必问:大模型Agent与MCP区别全解(非常详细),Java转AI从入门到精通,收藏这一篇就够了!
  • 解决Quartus 18.1下载失败的5个常见问题:以USB-Blaster配置为例
  • 2026年郑州靠谱GEO服务商深度解析:从技术实力到效果落地的选型逻辑 - 小白条111
  • 3步终结标签页灾难:Open Multiple URLs让浏览器效率提升10倍的秘密
  • 从SQL注入到XSS:实战Web安全渗透测试
  • Spring Boot应用Heapdump文件泄露实战:从下载到利用CF工具接管阿里云主机
  • 51汇编仿真Proteus8.15实战篇一(附源码)
  • 别再死记硬背公式了!用MATLAB/Python生成通信仿真噪声(含复高斯噪声代码)
  • 25555
  • 涛的天道观【其九十一】真正的能力
  • Python 装饰器实战:从入门到精通
  • 如何提升病理切片诊断质量?解决 HE 染色对比度低与背景干扰的品牌选型指南
  • Z-Image模型.NET开发:C#调用AI绘图API实战
  • Qwen3-VL:30B模型服务网格:Istio流量管理
  • 别再只用UART了!手把手教你用SIT3232E和SIT3485E搞定RS232/RS485电路(附完整原理图)
  • openstack下载iaas-install-nova-compute.sh出错怎么办
  • IP的纯净度和覆盖率,为何决定代理效果?