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项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom