Qt中自带的例子。详细情况,还得仔细看一下Model/View那一章。

The Spin Box Delegate example shows how to create an editor for a custom delegate in the model/view framework by reusing a standard Qt editor widget.

The model/view framework provides a standard delegate that is used by default with the standard view classes. For most purposes, the selection of editor widgets available through this delegate is sufficient for editing text, boolean values, and other simple data types. However, for specific data types, it is sometimes necessary to use a custom delegate to either display the data in a specific way, or allow the user to edit it with a custom control.

This concepts behind this example are covered in the Delegate Classes chapter of the Model/View Programming overview.

SpinBoxDelegate Class Definition

The definition of the delegate is as follows:

class SpinBoxDelegate : public QItemDelegate

{

Q_OBJECT

public:

SpinBoxDelegate(QObject *parent = 0);

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;

};

The delegate class declares only those functions that are needed to create an editor widget, display it at the correct location in a view, and communicate with a model. Custom delegates can also provide their own painting code by reimplementing the paintEvent() function.

SpinBoxDelegate Class Implementation

Since the delegate is stateless, the constructor only needs to call the base class's constructor with the parent QObject as its argument:

SpinBoxDelegate::SpinBoxDelegate(QObject *parent)

: QItemDelegate(parent)

{

}

Since the delegate is a subclass of QItemDelegate, the data it retrieves from the model is displayed in a default style, and we do not need to provide a custom paintEvent().

The createEditor() function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive.

QWidget *SpinBoxDelegate::createEditor(QWidget *parent,

const QStyleOptionViewItem &/* option */,

const QModelIndex &/* index */) const

{

QSpinBox *editor = new QSpinBox(parent);

editor->setMinimum(0);

editor->setMaximum(100);

return editor;

}

We install an event filter on the spin box to ensure that it behaves in a way that is consistent with other delegates. The implementation for the event filter is provided by the base class.

The setEditorData() function reads data from the model, converts it to an integer value, and writes it to the editor widget.

void SpinBoxDelegate::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);

}

Since the view treats delegates as ordinary QWidget instances, we have to use a static cast before we can set the value in the spin box.

The setModelData() function reads the contents of the spin box, and writes it to the model.

void SpinBoxDelegate::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);

}

We call interpretText() to make sure that we obtain the most up-to-date value in the spin box.

The updateEditorGeometry() function updates the editor widget's geometry using the information supplied in the style option. This is the minimum that the delegate must do in this case.

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,

const QStyleOptionViewItem &option, const QModelIndex &/* index */) const

{

editor->setGeometry(option.rect);

}

More complex editor widgets may divide the rectangle available in option.rect between different child widgets if required.

The Main Function

This example is written in a slightly different way to many of the other examples supplied with Qt. To demonstrate the use of a custom editor widget in a standard view, it is necessary to set up a model containing some arbitrary data and a view to display it.

We set up the application in the normal way, construct a standard item model to hold some data, set up a table view to use the data in the model, and construct a custom delegate to use for editing:

int main(int argc, char *argv[])

{

QApplication app(argc, argv);

QStandardItemModel model(4, 2);

QTableView tableView;

tableView.setModel(&model);

SpinBoxDelegate delegate;

tableView.setItemDelegate(&delegate);

The table view is informed about the delegate, and will use it to display each of the items. Since the delegate is a subclass of QItemDelegate, each cell in the table will be rendered using standard painting operations.

We insert some arbitrary data into the model for demonstration purposes:

for (int row = 0; row < 4; ++row) {

for (int column = 0; column < 2; ++column) {

QModelIndex index = model.index(row, column, QModelIndex());

model.setData(index, QVariant((row+1) * (column+1)));

}

}

Finally, the table view is displayed with a window title, and we start the application's event loop:

tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));

tableView.show();

return app.exec();

}

Each of the cells in the table can now be edited in the usual way, but the spin box ensures that the data returned to the model is always constrained by the values allowed by the spin box delegate.

Qt表格中以旋转框的形式数据交互相关推荐

  1. 要点初见:Python+OpenCV校正并提取表格中的各个框

    最近做了个手写汉字简历识别比赛,需要先提取表格中含有指定信息的各个框,再用TensorFlow对框中的信息进行汉字.数字.英文识别.代码已开源:https://github.com/BingLiHan ...

  2. 选下拉框的的值对应上传相应的图片_如何在excel中实现,选择下拉菜单某一项,该表格中就出现选项对应的数据?(excel表格制作选择数据)...

    怎样从多个excel表格中提取数据,做数据分析图呢 1. 数据的.录入.表格的设置,效果如示. 2.如图所示,选进行分析的图据范围 3.如图所示,点击菜单栏目上的"插入",选择&q ...

  3. wps html如何保存excel,wps excel导入html表格数据格式转换-怎样将wps表格中的数值转换为文本形式...

    怎样将wps表格中的数值转换为文本形式 将网上数入wps表格中的方法: 1.新建WPS表格,命名为<学生参赛信息.et>. 2.执行"数据"→"导入数据&qu ...

  4. 选下拉框的的值对应上传相应的图片_excel表格下拉菜单调用对应数据,如何在excel中实现,选择下拉菜单某一项,该表格中就出现选项对应的数据?...

    如何在excel中实现,选择下拉菜单某一项,该表格中就出现选项对应的数据? 选中这几列 打开菜单"数据"-"筛选"-"自动筛选"就是了. 另 ...

  5. xlsx表格怎么做汇总统计_Excel表格中如何快速汇总多个数据表中的数据

    原标题:Excel表格中如何快速汇总多个数据表中的数据 在Excel工作表中,如果需要汇总报告多个单独单元格的结果,可以将这些单元格中的数据合并到一个主工作表中.这些工作表可以与主工作表在同一个工作簿 ...

  6. python作中国地图背景气泡图_exce表格中怎么制作中国地图背景数据气泡图

    exce表格中怎么制作中国地图背景数据气泡图 exce表格中怎么制作中国地图背景数据气泡图?excel表格中想要在中国地图上显示气泡来看看地区分布情况,该怎么设置中国地图气泡图表呢?下面我们就来看看详 ...

  7. Excel表格中如何找出并替换数据中某一固定范围内的值(包括空值)

    Excel表格中如何找出并替换数据中某一固定范围内的值(包括空值) 一.问题介绍 二.方法介绍 1.选中数据 2.按住Ctrl+H键,调出替换窗口 3.输入 " * ",并点击查找 ...

  8. ajax数据交互代码,Django中使用jquery的ajax进行数据交互的实例代码

    jquery框架中提供了$.ajax.$.get.$.post方法,用于进行异步交互,由于Django中默认使用CSRF约束,推荐使用$.get 示例:实现省市区的选择 最终实现效果如图: 将jque ...

  9. ajax与后台php,怎么在thinkPHP5中使用ajax实现与后台数据交互

    怎么在thinkPHP5中使用ajax实现与后台数据交互 发布时间:2021-03-20 17:20:01 来源:亿速云 阅读:87 作者:Leah 这篇文章给大家介绍怎么在thinkPHP5中使用a ...

最新文章

  1. js layui 弹出子窗体_Layui中JS实现弹出层的应用
  2. 用py2exe打包成一个exe文件
  3. zoj 1074 To the MAX
  4. 文件上传存至oracle,fileupload上传文件存储到oracle Blob字段中
  5. oracle 10g 学习之视图、序列、索引、同义词(9)
  6. mybatis整合spring,使用org.mybatis.spring.mapper.MapperScannerConfigurer扫描出现问题
  7. android9 三星 港版,三星S9+官方港版安卓9完整固件系统升级包:TGY-G9650ZHU5CSFB
  8. 实时音频编解码之七 预加重
  9. centos7 RAID磁盘阵列卡驱动安装图文教程
  10. 天狼星网络验证源码/官方正版/内附搭建教程
  11. PDF修改文字的步骤
  12. 在线banner制作网站
  13. 软考试题中经常混淆的概念
  14. UE4如何实现裸眼3D效果 3D立体效果
  15. java这一年第几天_java 输入年月日,计算该日是这一年的第几天
  16. 基于主定理以及递推树求解递归算法的时间复杂度
  17. java.lang.RuntimeException: Parcel: unable to marshal value com.
  18. php7的浮点数,php7.1浮点数运算问题
  19. 【Ryo】不定期更新的藏宝阁——发现GitHub上的宝贝
  20. NMAKE简要教程1:环境配置

热门文章

  1. Sequelize小记
  2. P1103 书本整理
  3. EasyPlayer Android RTSP播放器延迟再优化策略
  4. 小强同学,去当老师吧!
  5. 关于Tomcat 的一些配置和启动
  6. Oracle 11g-R2 SQL Developer连接MSSQL2008
  7. Ext.Net 最新版(2011-06-24)License 问题
  8. 设计模式之单例模式8种实现方式,其八:枚举方式
  9. ApiPost报TypeError: Cannot read property ‘oauth‘ of undefined的解决方案
  10. java8 lambda this_java8里lambda里的 this 为什么会指向 lamdba 所在的外部类