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

相关文章

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A