QStandardItemModel-Delegate

//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();
}
QStandardItemModel-Delegate的更多相关文章
- QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)
今天介绍一下一个小东西 — 如何让QComboBox实现复选功能? 需求: 下拉列表有复选功能 不可编辑 显示所有选中项 关于QComboBox的复选功能有几种方案: QStandardIte ...
- QStandardItemModel简单好用,QTableView带进度条
类QabstractItemModel,QabstractListModel,QAbstractTableModel不保存数据,用户需要从这些类派生出子类,并在子类中定义某种数据结构来保存数据.与此不 ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- 9.Delegate类
Concepts 不像MVC模式,Model/View模式并没有单独用来处理事件和用户交互的组件(controller).通常,视图负责向用户呈现模型中的数据,并处理用户的输入.有时,为了让 ...
- MVC架构之delegate
Qt的MVC架构可以实现很多数据显示的功能,本次主要对代理进行一个总结: 重实现QStyledItemDelegate类,实现自定义类. (1)ComboxDelegate.h #ifndef COM ...
- [.NET] C# 知识回顾 - 委托 delegate (续)
C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...
- [C#] C# 知识回顾 - 委托 delegate
C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...
- iOS 键盘添加完成按钮,delegate和block回调
这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...
- C# 委托Delegate(一) 基础介绍&用法
本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...
- Jquery中的bind(),live(),delegate(),on()绑定事件方式
博客转载为作者:枫上善若水http://www.cnblogs.com/xilipu31/p/4105794.html 前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到d ...
随机推荐
- c/c++ string.h
操作数的都是 ( char * )型,操作时不考虑末尾的'\0',操作数必须是指向字符串的指针("a"),不能是字符('a'). size_t strlen( const char ...
- .NET LINQ 数据排序
数据排序 排序操作按一个或多个特性对序列的元素进行排序. 第一个排序条件对元素执行主要排序. 通过指定第二个排序条件,可以对各个主要排序组中的元素进行排序. 方法 方法名 说明 C# 查 ...
- AngularJS 模块
模块定义了一个应用程序. 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通常属于一个模块. 你可以通过 AngularJS 的 angular.module 函数来创建模块: &l ...
- [LeetCode] Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)
问题: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 如何自定义jupyter notebook的主题
临时性的改变一个jupyter的主题 参考这个实现,只要在notebook里运行这段代码就行了,能让所有的cell都能够显示黑色背景 一个更为完备的工具 参考这个方案: 安装jupyter-theme ...
- python下编译py成pyc和pyo
python下编译py成pyc和pyo 其实很简单, 用 python -m py_compile file.py python -m py_compile /root/src/{file1,f ...
- Javassist 字节码操作
1.读写字节码 Javassist是用来处理java字节码的类库.字节码保存在二进制文件中称为类文件.每个类文件夹包括一个java类或接口. Javasssist.CtClass这个类是一个类文件的抽 ...
- Python lambda函数
python允许定义单行的小函数,定义lambda函数的形式如下: lambda 参数:表达式lambda函数默认返回表达式的值,可接收任意个参数,包括可选参数,但是表达式只有一个.
- PHP-Redis扩展使用手册(一)
//初始化redis实例 $redis = new Redis(); /* connect . open 链接redis * @param string host redis服务器地址 * @para ...