Results 1 to 7 of 7

Thread: QModelIndex value for the root of a tree model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex value for the root of a tree model

    Quote Originally Posted by Corny View Post
    To get the index value of a top level item, I need to send the row and column of the item and the index value of the item's parent (which technically is the root) to the index function. That obviously results in an invalid index value being returned.
    Not obvious at all, this should return a valid index if the model has such a cell:
    Qt Code:
    1. if (model->rowCount(QModelIndex()) > 0 && model->columnCount(QModelIndex()) > 0) {
    2. QModelIndex topLevelRow0Column0 = model->index(0, 0, QModelIndex());
    3. Q_ASSERT(topLevelRow0Column0.isValid());
    4. }
    To copy to clipboard, switch view to plain text mode 
    If the index is not valid despite this cell being part of the model, then there must a problem with the model's index() implementation.

    The index() and the parent() method are the two tricky ones when implementing a tree model, so things can go wrong there.

    See https://wiki.qt.io/Model_Test for a simple standard test harness for models and also check out https://www.kdab.com/development-res...ools/gammaray/ (has a model inspector plugin)

    Cheers,
    _

  2. The following 2 users say thank you to anda_skoa for this useful post:

    Corny (20th March 2019), d_stranz (21st March 2019)

Similar Threads

  1. Replies: 15
    Last Post: 18th February 2016, 09:19
  2. Shows the root of a tree
    By Alundra in forum Qt Programming
    Replies: 12
    Last Post: 4th March 2015, 06:26
  3. Replies: 1
    Last Post: 29th August 2013, 05:41
  4. Not able to remove the root of the tree widget.
    By aurora in forum Qt Programming
    Replies: 1
    Last Post: 30th December 2011, 13:04
  5. QTreeView: include root in tree?
    By meter in forum Newbie
    Replies: 2
    Last Post: 14th June 2010, 12:59

Tags for this Thread

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.