PDA

View Full Version : Treeview and custom model



steg90
15th May 2007, 13:03
Hi,

I've started a custom model for a treeview, the code for the model is as follows:



#include "dacantreemodel.h"

DACanTreeModel::DACanTreeModel(QObject *parent)
: QAbstractItemModel(parent)
{
}

DACanTreeModel::~DACanTreeModel()
{
}

QModelIndex DACanTreeModel::index( int row, int column, const QModelIndex& parent) const
{
return QModelIndex();
}


int DACanTreeModel::rowCount(const QModelIndex &parent) const
{
return m_oTreeItems.count();
}

int DACanTreeModel::columnCount(const QModelIndex &parent) const
{
return 4;
}

QVariant DACanTreeModel::data(const QModelIndex &index, int role) const
{
QVariant data;

if (!index.isValid())
return QVariant();
if( role != Qt::DisplayRole )
return QVariant();
if(index.column() > 4 )
{
return QVariant();
}
if( role == Qt::DisplayRole )
data = "Test";

return data;
}

QVariant DACanTreeModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
return QVariant();

if (orientation == Qt::Horizontal)
return m_Headers[section];
else
return QVariant();
}


QModelIndex DACanTreeModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}

void DACanTreeModel::setHeaders( QStringList& headers )
{
m_Headers = headers;
}

void DACanTreeModel::addData( QString strId )
{
CMessage* pMess = new CMessage();
m_oTreeItems.insert( strId, (QObject*)pMess );
}



I have a QTreeView which sets the model to the above. When I call addData I want an item to be added to the tree but nothing is happening, I guess I'm missing something here?
In the data function I'm just setting it to 'Test' just to see if this would appear. I have not implemented the index() or the parent() method yet. How do I get something to be put into the tree?

Regards,
Steve

spud
15th May 2007, 13:14
void DACanTreeModel::addData( QString strId )
{
beginInsertRows(QModelIndex(), rowCount(), rowCount ());
CMessage* pMess = new CMessage();
m_oTreeItems.insert( strId, (QObject*)pMess );
endInsertRows();
}

steg90
15th May 2007, 14:05
Thanks for that Spud,

To no avail though, nothing is shown, in fact, setting a breakpoint on the data method doesn't break?

I'm clueless?!?

Regards,
Steve

steg90
15th May 2007, 14:14
I've finally got it to break in on the data method by modifying the index method.

I now have :



return createIndex( row, column );


Not really sure what this does, the modelIndex confuses me?!?

Regards,
Steve

jpn
15th May 2007, 14:15
Remove DACanTreeModel::index(). It's incorrect and makes the model to not work properly. There should be no need to reimplement it.

Edit: Sorry, forget what I just said. For some reason I read it as QAbstractTableModel. Some other thread must have confused me. For QAbstractItemModel you naturally need to implement index() since it's pure virtual. But do you really need a QAbstractItemModel? In some other thread your data looked flat to me, so maybe you should consider using QAbstractTableModel which makes things easier for you.

spud
15th May 2007, 14:37
At the moment your model is flat and you can access any item with (row, column), thus your createIndex function can be very simple. If you're intending to make a truly hierarchical model, that won't be enough, though and you will have to store more information in the QModelIndex::internalPointer(). For example you could store a pointer to the parent item.
If you really need a hierarchical model I suggest you take look at the Simple Tree Model Example. Otherwise it will be a lot easier to go with the QAbstractTableModel as jpn suggested.

steg90
15th May 2007, 14:37
Hi JPN,

Don't think the data is flat, well, not 2d, as the first column will show message nodes with their corresponding signal nodes. Unless I'm mistaken, QAbstractTableModel doesn't allow this?

Please see attachment of what I'm trying to do.

Regards,
Steve

jpn
15th May 2007, 14:49
Don't think the data is flat, well, not 2d, as the first column will show message nodes with their corresponding signal nodes. Unless I'm mistaken, QAbstractTableModel doesn't allow this?

Please see attachment of what I'm trying to do.
Yes, you're right. The model needs to handle parent-child relationships. Well, good luck with implementing a tree model then! Model Test (http://labs.trolltech.com/page/Projects/Itemview/Modeltest) might be handy for checking out the most common mistakes. ;)

steg90
15th May 2007, 14:54
Thanks JPN,

I've decided to go for the table model as I don't really need to display the message name and data as the messages are chosen from a previous dialog, this will be the easier option for me as I'm totally confused about the modelIndex and the internalPointer() function?!

Kind regards,
Steve