//delete.h
#ifndef DELEGATE_H
#define DELEGATE_H
#include<QItemDelegate>
#include<QModelIndex>
#include<QObject>
#include<QSize>
#include<QSpinBox>
#include<QWidget> class Delegate : public QItemDelegate
{
Q_OBJECT
public:
explicit Delegate(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; }; #endif // DELEGATE_H

  

//dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include"delegate.h" #include <QDialog>
#include<QtCore>
#include<QtGui>
#include<QModelIndex>
#include<QStandardItemModel> namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = 0);
~Dialog(); private:
Ui::Dialog *ui;
QStandardItemModel *model;
Delegate *mydelegate;
}; #endif // DIALOG_H

  

//delegate.cpp
#include "delegate.h" Delegate::Delegate(QObject *parent):QItemDelegate(parent)
{ } QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
} void Delegate::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 Delegate::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 Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}

  

//dialog.cpp
#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
mydelegate = new Delegate(this); model = new QStandardItemModel(4,2,this);
for(int row = 0; row < 4; ++row)
{
for(int col = 0; col < 2; ++col)
{
QModelIndex index = model->index(row,col,QModelIndex());
model->setData(index,0);
}
} ui->tableView->setModel(model);
ui->tableView->setItemDelegate(mydelegate);
} Dialog::~Dialog()
{
delete ui;
}

  

//main.cpp
#include "dialog.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show(); return a.exec();
}

  

最新文章

  1. Linux iptables原理--数据包流向
  2. ZeroMQ接口函数之 :zmq_msg_data - 返回消息内容的指针
  3. jQuery学习-css、class操作、动画方法的运用、jQ操作Dom节点
  4. java 25 - 4 网络编程之 UDP协议传输的代码优化
  5. 剑指Offer 连续子数组的最大和
  6. 记一个eclipse 错误 Undefined variable from import: randrange
  7. Atitit.软件开发提升稳定性总结
  8. php--.prop()
  9. Delphi自定义窗口过程WinProc
  10. 交互式shell和非交互式shell、登录shell和非登录shell的区别
  11. 【转】Android SwitchButton(滑动开关)
  12. Oracle中的job(转载)
  13. jquery.lazyload.js图片延迟加载(懒加载)--转载
  14. SQL点滴25—T-SQL面试语句,练练手
  15. 安装老版本redis .NET 客户端
  16. Django入门与实践-第12章:复用模板(完结)
  17. 内置装饰器二:@property
  18. 提高myEclipse的开发效率和外观,这些你都设置了吗?
  19. Mirror--镜像相关操作
  20. webkit架构

热门文章

  1. php随机生成验证码代码
  2. nginx 配置https upstream 跳转失败
  3. 第二十九篇:使用SOUI的SMCListView控件
  4. 【转】一台电脑同时运行多个tomcat配置方法
  5. http返回码301、302、307、305含义和区别
  6. AngularJS动画
  7. [自动运维]oracle自动备份
  8. dedecms头部常用:标题,栏目描述,关键词
  9. java的poi技术读,写Excel[2003-2007,2010]
  10. php递归获取顶级父类id