次の簡単なモデルの例があります。
#include <QtGui/QApplication>
#include <QtGui/QTreeView>
#include <QAbstractItemModel>
class TestModel : public QAbstractItemModel
{
public:
TestModel()
{
SetUpData();
}
virtual QModelIndex index(int row, int column, const QModelIndex& parent) const
{
if ( parent.isValid())
{
// child
return createIndex( row, column, (void*)&mData[row].mChildren[column]);
}
// root
return createIndex( row, column, (void*)&mData[row]);
}
virtual QModelIndex parent(const QModelIndex& child) const
{
ModelData* data = (ModelData*)child.internalPointer();
// find out where "data" is in the mData vector structure
for ( size_t i=0; i<mData.size(); ++i )
{
for ( size_t j=0; j<mData[i].mChildren.size(); ++j )
{
if ( &mData[i].mChildren[j] == data )
{
// I think this is correct, return the parent row at col 0?
return createIndex( i, 0, (void*)&mData[i].mChildren[j]);
}
}
}
return QModelIndex();
}
virtual int rowCount(const QModelIndex& parent) const
{
if ( parent.isValid() )
{
// Per root node size
return mData[parent.row()].mChildren.size();
}
// Root size
return mData.size();
}
virtual int columnCount(const QModelIndex& parent) const
{
// The "parent" nodes should have two columns, the children should have 1
if ( parent.isValid() )
{
// Root
return 1;
}
// Children
return 2;
}
virtual QVariant data(const QModelIndex& index, int role) const
{
if ( role == Qt::DisplayRole && index.isValid() )
{
if ( index.isValid() )
{
// I think col and row are the wrong way around, but will crash if swapped
return mData[index.column()].mChildren[index.row()].mName;
}
else
{
// this never happens because "RootN" is never displayed
return mData[index.column()].mName;
}
}
return QVariant();
}
private:
// The "real" data that this Qt model is providing an interface to
struct ModelData
{
QString mName;
std::vector< ModelData > mChildren;
};
std::vector< ModelData > mData;
void SetUpData()
{
for ( int i=0; i<3; ++i )
{
ModelData root;
root.mName = "Root" + QString::number( i+1 );
for ( int j=0; j<10; ++j )
{
ModelData node;
node.mName = "Node" + QString::number( j+1 );
root.mChildren.push_back( node );
}
mData.push_back( root );
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestModel* model = new TestModel();
QTreeView* tv = new QTreeView();
tv->setModel( model );
tv->show();
int ret = a.exec();
delete tv;
delete model;
return ret;
}
ここでやろうとしているのは、次の構造を持つモデルを作成することです。
Root1
|-Node1-10
|
Root2
|-Node1-10
|
Root3
|-Node1-10
しかし、私はこれで終わります:
インデックス/親とデータがどのように機能するはずなのか、本当に混乱しています。私の出力が間違っているので、明らかに私はそれを理解していません。
たとえば、Root1、Node3 によって、row = 0、col = 2 で index が呼び出されると思いました。その時点で、createIndex を 0,2 で呼び出し、その要素へのポインターを呼び出しますか?
また、parent() の場合、親が有効な場合は QModelIndex を返します。これは RootN 要素であることを意味し、ルート要素でない場合は createIndex を返して親へのインデックスを作成します。
最後に、データについては、これは特定の行/列の UI の表示文字列を返すだけだと思いましたか?