珠海网站策划公司,百度快照是干什么的,装修设计公司官网,公司网站建设多少费用兴田德润在哪里参考#xff1a;QT(7)-初识委托_qt 委托-CSDN博客
一、
1、
模型#xff1a;负责“组织”数据#xff1b;
视图#xff1a;负责“显示”数据#xff1b;
委托#xff1a;负责“修改”数据#xff1b; 2、委托#xff1a;在QT的MV模型中#xff0c;处理特定类型的…参考QT(7)-初识委托_qt 委托-CSDN博客
一、
1、
模型负责“组织”数据
视图负责“显示”数据
委托负责“修改”数据 2、委托在QT的MV模型中处理特定类型的数据控制数据的外观和行为
定制 数据 的“显示”和“编辑”
编辑日期,数值、渲染颜色,字体、
支持文本编辑器,下拉列表编辑器等、
更新编辑器的尺寸、证编辑器中的数据是否合法 3、委托应用场景
3.1表格和列表视图编辑单元格的数据、定制单元格外观
3.2 属性编辑器创建自定义的属性编辑器
3.3 文件对话框定制文件列表的外观 二、QT中的委托类 抽象基类
QAbstractItemDelegate-》QItemDelegate-》QStyledItemDelegate
QStyledItemDelegate使用Qt Style Sheets来渲染单元格中的数据这样可以更好地与应用程序的外观保持一致。 三、委托类 关键函数
3.1 virtual函数
createEditor创建用于编辑特定单元格中数据的 编辑器
setEditorData编辑器被创建后被调用用于初始化设置编辑器的值。
setModelData在编辑器编辑完成后被调用用于将编辑器中数据更新到数据模型中
updateEditorGeometry用于设置编辑器的位置和大小。
一般通过继承 QStyledItemDelegate以上4个函数便可以实现简单的自定义委托。
3.2 其他函数
destroyEditor
editorEvent
helpEvent
sizeHint
这些函数并没有重要和非重要之分需要根据不同的需求实现不同的函数达到想要的效果。
四、使用委托 实现功能 4.1 委托 实现功能1创建QSpinBox 编辑器、设置编辑器值、将编辑器中值保存的数据模型、设置编辑器位置 delegate.h文件#ifndef DELEGATE_H
#define DELEGATE_H#include QStyledItemDelegate//! [0]
class SpinBoxDelegate : public QStyledItemDelegate
{Q_OBJECTpublic:SpinBoxDelegate(QObject *parent nullptr);QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem option,const QModelIndex index) const override;void setEditorData(QWidget *editor, const QModelIndex index) const override;void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex index) const override;void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem option,const QModelIndex index) const override;
};
//! [0]#endifdelegate.cpp文件#include delegate.h
#include QSpinBox//! [0]
SpinBoxDelegate::SpinBoxDelegate(QObject *parent): QStyledItemDelegate(parent)
{
}
//! [0]//! [1]
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem /* option */,const QModelIndex /* index */) const
{QSpinBox *editor new QSpinBox(parent);editor-setFrame(false);editor-setMinimum(0);editor-setMaximum(100);return editor;
}
//! [1]//! [2]
void SpinBoxDelegate::setEditorData(QWidget *editor,const QModelIndex index) const
{int value index.model()-data(index, Qt::EditRole).toInt();QSpinBox *spinBox static_castQSpinBox*(editor);spinBox-setValue(value);
}
//! [2]//! [3]
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex index) const
{QSpinBox *spinBox static_castQSpinBox*(editor);spinBox-interpretText();int value spinBox-value();model-setData(index, value, Qt::EditRole);
}
//! [3]//! [4]
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem option,const QModelIndex /* index */) const
{editor-setGeometry(option.rect);
}
//! [4]createEditor函数中定义了一个 QSpinBox类型的编辑器并对其进行了简单的初始化和设置
setEditorData函数中将模型中index位置的数据拿了出来并保存在了value中并将value设置到了编辑器中
setModelData函数中将编辑器中的数据取出来设置到模型中
updateEditorGeometry中对位置进行了设置editor-setGeometry(option.rect);不执行这个生成的控件在坐标原点 4.2 委托 实现功能2创建QDateEdit 编辑器、设置编辑器值、将编辑器中值保存的数据模型、设置编辑器位置 delegate.cpp文件#include delegate.h
#include QSpinBox
#include QDateEditQDateEdit::QDateEdit(QObject *parent): QStyledItemDelegate(parent)
{
}QWidget *QDateEdit::createEditor(QWidget *parent,const QStyleOptionViewItem /* option */,const QModelIndex /* index */) const
{
// QSpinBox *editor new QSpinBox(parent);
// editor-setFrame(false);
// editor-setMinimum(0);
// editor-setMaximum(100);auto *editor new QDateEdit(parent);editor-setDisplayFormat(yyyy-MM-dd);return editor;
}void QDateEdit::setEditorData(QWidget *editor,const QModelIndex index) const
{
// int value index.model()-data(index, Qt::EditRole).toInt();
// QSpinBox *spinBox static_castQSpinBox*(editor);
// spinBox-setValue(value);auto value index.model()-data(index, Qt::EditRole).toDate();QDateEdit *dateEdit static_castQDateEdit*(editor);dateEdit-setDate(value);
}void QDateEdit::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex index) const
{
// QSpinBox *spinBox static_castQSpinBox*(editor);
// spinBox-interpretText();
// int value spinBox-value();
// model-setData(index, value, Qt::EditRole);auto dateEdit static_castQDateEdit*(editor);auto value dateEdit-date();model-setData(index,value,Qt::EditRole);
}void QDateEdit::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem option,const QModelIndex /* index */) const
{editor-setGeometry(option.rect);
}
五、
按照这个思路那么只要改一下里面的编辑器就能实现不同的委托。想要实现不同的委托只要继承QStyledItemDelegate类并实现相应的函数就可以实现不同的委托。
密码委托下拉框委托QComboBox颜色选择委托图标委托等等甚至包括一些你自己定义的控件的委托。