QStyledItemDelegate自定义代理组件

2023-12-29 22:18

本文主要是介绍QStyledItemDelegate自定义代理组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、继承QStyledItemDelegate自定义类组件

2、重写函数

3、通过在QTableView中插入自定义代理组件为例

继承QStyledItemDelegate写的头文件

#ifndef CUSTOMPUSHBOTTONDELEGATE_H
#define CUSTOMPUSHBOTTONDELEGATE_H#include <QObject>
#include <QStyledItemDelegate>
#include <QPushButton>
class CustomPushBottonDelegate : public QStyledItemDelegate
{Q_OBJECTpublic:CustomPushBottonDelegate();~CustomPushBottonDelegate();QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;void setEditorData(QWidget *editor, const QModelIndex &index) const;void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;private:};#endif // CUSTOMPUSHBOTTONDELEGATE_H

函数实现

#include "customspinboxdelegate.h"CustomSpinBoxDelegate::CustomSpinBoxDelegate()
{
}CustomSpinBoxDelegate::~CustomSpinBoxDelegate()
{}//创建代理编辑组件
QWidget* CustomSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QSpinBox* editor = new QSpinBox(parent);editor->setFrame(false);editor->setMinimum(0);editor->setMaximum(100000);return editor;
}//从数据模型获取数据,显示到代理组件中
void CustomSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{int value = index.model()->data(index, Qt::EditRole).toInt();QSpinBox* spinBox = static_cast<QSpinBox*>(editor);spinBox->setValue(value);
}//将代理组件的数据保存到数据模型中
void CustomSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QSpinBox* spinBox = static_cast<QSpinBox*>(editor);spinBox->interpretText();int value = spinBox->value();model->setData(index, value, Qt::EditRole);
}//设置组件大小
void CustomSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}

main中过程

头文件

#ifndef QVARIANTCUSTOM_H
#define QVARIANTCUSTOM_H#include <QtWidgets/QWidget>
#include "ui_qvariantcustom.h"
#include <QDebug>
#include "customspinboxdelegate.h"
#include <QStandardItemModel>
#include "customcomboboxdelegate.h"
#include "custompushbottondelegate.h"class QVariantCustom : public QWidget
{Q_OBJECTpublic:QVariantCustom(QWidget *parent = 0);~QVariantCustom();QStandardItemModel* pModel = nullptr;
private:Ui::QVariantCustomClass ui;CustomSpinBoxDelegate spinBoxDelegate;CustomComboBoxDelegate comboBoxDelegate;CustomPushBottonDelegate pushButtonDelegate;
};#endif // QVARIANTCUSTOM_H

主函数

#include "qvariantcustom.h"QVariantCustom::QVariantCustom(QWidget *parent): QWidget(parent)
{ui.setupUi(this);pModel = new QStandardItemModel(2, 3, this);ui.tableView->setModel(pModel);ui.tableView->setItemDelegateForColumn(0, &spinBoxDelegate);/*ui.tableView->setItemDelegateForColumn(1, &comboBoxDelegate);ui.tableView->setItemDelegateForColumn(2, &pushButtonDelegate);*/
}QVariantCustom::~QVariantCustom()
{}

 

这篇关于QStyledItemDelegate自定义代理组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/550897

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并