西安网站设计开发前端开发培训费用
news/
2025/9/29 0:41:45/
文章来源:
西安网站设计开发,前端开发培训费用,网站的费用可以做无形资产,获取wordpress所有分类名字和id一、基本概念
与MVC模式不同#xff0c;MV视图架构中没有包含一个完全分离的组件来处理与用户的交互。
一般地#xff0c;视图用来将模型中的数据显示给用户#xff0c;也用来处理用户的输入。为了获得更高的灵活性#xff0c;交互可以由委托来执行。
这些组件提供了输入…一、基本概念
与MVC模式不同MV视图架构中没有包含一个完全分离的组件来处理与用户的交互。
一般地视图用来将模型中的数据显示给用户也用来处理用户的输入。为了获得更高的灵活性交互可以由委托来执行。
这些组件提供了输入功能而且也负责渲染一些视图中的个别项目。控制委托的接口在QAbstractItemDelegate类中定义。
委托通过实现paint()和sizeHint()函数来使它们可以渲染自身的内容。然而 简单的基于部件的委托可以通过子类化QItemDelegate来实现而不需要使用QAbstractItemDelegate这样可以使用这些函数的默认实现。
委托的编辑器可以通过两种方式实现一种是使用不见来管理编辑过程另一种是直接处理事件。下面会讲解第一种方式。 可以参考一下Qt提供的Spin Box Delegate示例程序。 如果想要继承QAbstractItemDelegate来实现自定义的渲染操作也可以参考一下Pixelator示例程序。 另外也可以使用QStyledItemDelegate作为基类这样可以自定义数据的显示这个可以参考Star Delegate示例程序。这些示例程序都在Item View分类中。 Qt中的标准视图都使用QItemDelegate的实例来提供编辑功能这种委托接口的默认实现为QListView、QTableView和QTreeView等标准视图的每一个项目提供了普通风格的渲染。
标准视图中的默认委托会处理所有的标准角色具体的内容可以在QItemDelegate类的帮助文档中查看。可以使用itemDelegate()函数获取一个视图中使用的委托使用setItemDelegate()函数可以为一个视图安装一个自定义委托。
二、自定义委托
SpinBoxDelegate.h
#pragma once#include QSpinBox
#include QItemDelegateclass SpinBoxDelegate : public QItemDelegate {
public:using QItemDelegate::QItemDelegate;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;
};SpinBoxDelegate.cpp
#include SpinBoxDelegate.hQWidget *
SpinBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem option,const QModelIndex index) const
{auto editor new QSpinBox(parent);editor-setMinimum(0);editor-setMaximum(100);return editor;
}void
SpinBoxDelegate::setEditorData(QWidget *editor,const QModelIndex index) const
{int value index.model()-data(index, Qt::EditRole).toInt();auto spinBox static_castQSpinBox *(editor);spinBox-setValue(value);
}void
SpinBoxDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex index) const
{auto spinBox static_castQSpinBox *(editor);spinBox-interpretText();int value spinBox-value();model-setData(index, value, Qt::EditRole);
}void
SpinBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem option,const QModelIndex index) const
{(void)index;editor-setGeometry(option.rect);
}用法
auto delegate new SpinBoxDelegate(this);
tableView-setItemDelegate(delegate);
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/921316.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!