Help needed with QAbstractItemModel
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
Re: Help needed with QAbstractItemModel
Does the function signature match exactly, including constness and such?
Re: Help needed with QAbstractItemModel
Maybe you forgot about "const"?
Re: Help needed with QAbstractItemModel
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
Re: Help needed with QAbstractItemModel
May be you forgot/removed the line that sets the number of columns ?
Hth.
Re: Help needed with QAbstractItemModel
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();
}
1 Attachment(s)
Re: Help needed with QAbstractItemModel
Heres the tidied down code.
This should display a Dialog - with a blank tree and two colume headers
"Title" "Summary"
No headers so far.
Re: Help needed with QAbstractItemModel
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.
Re: Help needed with QAbstractItemModel
I wondered why that was coded in the original QT examples.
Yep agree. The inherited QObject should take care of memory garbage.
Re: Help needed with QAbstractItemModel
Quote:
Originally Posted by
Harvey West
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).
Re: Help needed with QAbstractItemModel
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.
Re: Help needed with QAbstractItemModel
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.