QT mvc 关于editable item书中写错了?

===============pro======================
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = editable

TEMPLATE = app
HEADERS += \
    treemodel.h

SOURCES += \
    main.cpp \
    treemodel.cpp
=============TREEMODEL.H=====================

#ifndef TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>

class TreeModel : public QAbstractItemModel
{
	Q_OBJECT
public:
	TreeModel();
	int rowCount ( const QModelIndex & parent ) const;
	int columnCount ( const QModelIndex & parent ) const;
	QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
	QModelIndex parent ( const QModelIndex & index ) const;
	QVariant data ( const QModelIndex & index, int role ) const;
	bool setData(const QModelIndex & index, const QVariant & value, int role);
	Qt::ItemFlags flags(const QModelIndex & index) const;

private:
	enum {N=15};
	int numbers[N];
};

#endif // TREEMODEL_H
==============main.cpp==================
#include "treemodel.h"
#include <QApplication>
#include <QTreeView>
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
	TreeModel  treeModel;
	QTreeView treeView(0);
	treeView.setModel( & treeModel );
	treeView.show();
	return app.exec();
}
==============TreeModel.cpp==================

#include "treemodel.h"
#include <QDebug>
TreeModel::TreeModel()
{
	int values[N]={36,10,26,3,7, 11,15, 1,2,3,4,5,6,7,8};
	for (int i=0; i<N; i++)
		numbers[i] = values[i];
}
int TreeModel::rowCount ( const QModelIndex & parent ) const
{
	if ( ! parent.isValid() )
		return 1;
	if (parent.internalId() < N/2 )
		return 2;
	return 0;
}
int TreeModel::columnCount ( const QModelIndex & parent ) const 
{
	return 1;
}
QModelIndex TreeModel::index ( int row, int column, const QModelIndex & parent ) const 
{
	if ( ! parent.isValid() )
        return createIndex(row, column, Q_NULLPTR);
	int parent_idx = parent.internalId();
	int idx = parent_idx * 2 + ( row + 1 );
	return createIndex(row, column, idx );
}
QModelIndex TreeModel::parent ( const QModelIndex & index ) const
{
	if (index.internalId() == 0) 
		return QModelIndex();
	
	int parent_idx = (index.internalId() - 1 )/2;
	return createIndex( (parent_idx+1) % 2, 0, parent_idx );
}
Qt::ItemFlags TreeModel::flags(const QModelIndex & index) const
{
	if ( index.internalId() < N/2 )
		return Qt::ItemIsSelectable |  Qt::ItemIsEnabled ;
	else
		return Qt::ItemIsSelectable |   Qt::ItemIsEnabled  | Qt::ItemIsEditable;
}
QVariant TreeModel::data ( const QModelIndex & index, int role  ) const 
{
	int value;
	switch (role) {
	case Qt::DisplayRole:
		value = numbers[ index.internalId() ];
		return QVariant( value );
	case Qt::EditRole:
		value = numbers[ index.internalId() ];
		return QVariant( value );
	}
	return QVariant();
}
bool TreeModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
    if (role != Qt::EditRole) return true;
	
	int id = index.internalId();
	while (1) {
		if ( id >= N/2 ) 
			numbers[id] = value.toInt();
		else 
			numbers[id]  = numbers[2 * id + 1] + numbers[ 2 * id + 2];
		
        QModelIndex idx = createIndex(1,0,id);//这句好象错了,行数都为1?
//        qDebug()<<"index:="<<index<<"||idx:="<<idx<<endl;
//        emit dataChanged(index,index);
//        emit dataChanged(idx,idx);
		if ( id == 0 ) break;
		id = ( id - 1 ) /2;
	};
	return true;
}
=========================code end=============================
以上源码为某书中的,但最后一个函数setData似乎错了。
 QModelIndex idx = createIndex(1,0,id);//这句好象错了,行数都为1?
把emit datachanged全注释掉也都可行,应该是默认事件在起作用。



                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值