Results 1 to 12 of 12

Thread: Help needed with QAbstractItemModel

  1. #1
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help needed with QAbstractItemModel

    Does the function signature match exactly, including constness and such?
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help needed with QAbstractItemModel

    Maybe you forgot about "const"?

  4. #4
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  5. #5
    Join Date
    May 2006
    Posts
    55
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help needed with QAbstractItemModel

    May be you forgot/removed the line that sets the number of columns ?
    Hth.

  6. #6
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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();
    }

  7. #7
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  9. #9
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help needed with QAbstractItemModel

    Quote Originally Posted by Harvey West View Post
    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).
    J-P Nurmi

  11. #11
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  12. #12
    Join Date
    Nov 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. QAbstractItemModel for dummies
    By morty in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 15:25
  2. Subclassing QAbstractItemModel
    By r366y in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2006, 08:46
  3. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16
  4. help needed regarding QWT
    By skatakam in forum Qwt
    Replies: 1
    Last Post: 21st March 2006, 12:47
  5. Replies: 8
    Last Post: 7th March 2006, 13:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.