网站首页 > 博客文章 正文
代理说明
代理使用的类为QStyledItemDelegate.自定义代理需要实现以下4个函数:
自定义代理四个函数的说明
四个函数的原型:
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
- createEditor函数返回的是QWidget,因此可创建的代理可以是多个控件的组合。
- 数据保存到模型
setModeldata函数在当前item失去焦点后被调用,而不是从下拉框中选中某项即可选中。另外,在关闭窗口后也会调用此函数,将最后的设置保存到模型中。
QStandardItemModel中有itemChanged信号,当当前item被修改后发射信号,信号的发送在setModeldata函数调用之前,因此也则可以在此保存模型中修改的参数。
自定义代理的关键代码
QWidget *QWComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("优");
editor->addItem("良");
editor->addItem("一般");
editor->addItem("不合格");
return editor;
}
//将模型的数据设置到代理控件中
void QWComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString str = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->setCurrentText(str);
}
//将代理控件的选择数据设置到模型中
void QWComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString str = comboBox->currentText();
model->setData(index, str, Qt::EditRole);
}
void QWComboBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
设置代理的关键代码
ui->tableView->setItemDelegate(QAbstractItemDelegate *delegate)//设置所有的单元格
ui->tableView->setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate);//设置固定的列的代理
ui->tableView->setItemDelegateForRow(int row, QAbstractItemDelegate *delegate)//设置固定行的代理
文章转自博客园(YueLiGo):https://www.cnblogs.com/wsw2022/p/17084405.html
Qt资料领取→Qt资料领取(视频教程+文档+代码+项目实战)
猜你喜欢
- 2024-10-24 Qt Examples——QSlider(qt qcompleter)
- 2024-10-24 正点原子I.MX6U嵌入式Qt开发指南:第七章《Qt控件 2》
- 2024-10-24 销售订单管理,Excel表格模板演示
- 2024-10-24 抓大放小,瞅瞅 Qt 的几个基础模块
- 2024-10-24 WPF --- 如何重写WPF原生控件样式?
- 2024-10-24 Python入坑系列-pyside6桌面编程之border边框
- 2024-10-24 1.3 MyFirstWidget代码讲解及实用编程技巧分享
- 2024-10-24 C#知识|.Net控件二次封装之ComboBox下拉框
- 2024-10-24 在WPF 中想要在表格的同一列上显示多个表头
- 2024-10-24 《XAML简介及常用控件》(xaml基本语法)
你 发表评论:
欢迎- 05-30springboot 集成redisson 以及分布式锁的使用
- 05-30去哪儿技术面:10亿数据如何最快速插入MySQL?
- 05-30redis介绍
- 05-30redission YYDS
- 05-30手把手教你springboot集成mybatis
- 05-30mybatis根据表逆向自动化生成代码:自动生成实体类、mapper文件
- 05-30越来越大的微信小程序
- 05-30SpringBoot之数据访问——访问SQL数据库!
- 422℃用AI Agent治理微服务的复杂性问题|QCon
- 420℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 401℃初次使用IntelliJ IDEA新建Maven项目
- 401℃Maven技术方案最全手册(mavena)
- 394℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 390℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 389℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 384℃IT全明星|IntelliJ IDEA学习笔记(四、idea中怎么创建maven项目)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)