===============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全注释掉也都可行,应该是默认事件在起作用。