PDA

View Full Version : Help needed with QAbstractItemModel



Harvey West
24th November 2006, 15:16
Hello

Before I start uploading a load of code... ;)

What would cause QAbstractItemModel::headerData not to get called.

To debug my code - I've gone back to the C:\Qt\4.1.0\examples\itemviews\simpletreemodel and modified this code to try and show a tree within a QDialog widget. But unable to see header columes. I've noticed that the TreeModel ::headerData never gets called after my modifications.

Can't figure out why this would be. Any ideas.

Harvey

jpn
24th November 2006, 15:38
Does the function signature match exactly, including constness and such?

jacek
24th November 2006, 15:39
Maybe you forgot about "const"?

Harvey West
24th November 2006, 15:51
I've not done that many changes to the orinal treemodel.cpp.

i.e.
remove "setupModelData" call from the constructor i.e. just wanted to get the colume headers working for now.

not changed
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
int role) const


still has the const. If that what you ment.


Harvey

lauranger
24th November 2006, 15:58
May be you forgot/removed the line that sets the number of columns ?
Hth.

Harvey West
24th November 2006, 16:02
Nope check that that gets called once - returns 2.


i.e.

TreeModel::TreeModel(const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
QList<QVariant> rootData;
rootData << "Title" << "Summary";
rootItem = new TreeItem(rootData);
//setupModelData(data.split(QString("\n")), rootItem);
}

int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}

Harvey West
24th November 2006, 16:34
Heres the tidied down code.

This should display a Dialog - with a blank tree and two colume headers
"Title" "Summary"

No headers so far.

jacek
24th November 2006, 16:56
You create the model on the stack, so it gets destroyed as soon as program flow leaves FindSceneDialog::FindSceneDialog().

If you make the model a child of treeView, it will be deleted automatically, so you won't have to bother about destructor.

Harvey West
24th November 2006, 17:07
I wondered why that was coded in the original QT examples.
Yep agree. The inherited QObject should take care of memory garbage.

jpn
24th November 2006, 17:51
I wondered why that was coded in the original QT examples.
That's because the TreeModel created in main(). QApplication::exec() in the end of the main() will be blocking as long as the application runs. The model will be automatically destructed as soon as the application exits and the tree model gets out of scope. Thanks to the fact that QApplication::exec() blocks as long as the application runs, it's safe to allocate objects on the stack in main().

It wouldn't make any difference in the behaviour if the model was allocated on the heap and was made as a child of the view. Then it would simply be deleted by the view (when it gets out of scope as it's allocated on the stack).

Harvey West
24th November 2006, 18:14
Cheers for the info. Removed the destructor anyway - just in case but no luck yet.

Can't see any other threads which help so far. humm.

fyi
I have not knowingly touched the attatched treeitem.h/treeitem.cpp examples so these should work in this context.

Harvey West
27th November 2006, 12:06
Got it to work at last.

Cheers for the Stack hint. I put model onto the heap instead of the stack.

i.e.
created a model member in FindSceneDialog.
TreeModel *model;

and change the FindDialoge constructor
TreeModel *model = new TreeModel(file.readAll());

seems to do the trick.

Harvey.