Results 1 to 2 of 2

Thread: Level of an item QStandardItemModel for tree

  1. #1
    Join Date
    Apr 2013
    Posts
    64
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Level of an item QStandardItemModel for tree

    I am using QStandardItemModel in a QTreeView and trying to connect the selectionChanged signal of SelectionModel to track the user selection. I was able get the row/column of the selection. How do I get the level of the selected Item?
    For example,

    Animal
    -FourLegged
    --Dog
    --Cat
    -TwoLegged
    --Human
    Bird
    -Flying
    --Parrot
    -NonFlying
    --Chicken

    Selecting Animal/Bird should return me 0.
    Selecting FourLegged/TwoLegged/Flying/NonFlying should return me 1
    Selecting Cat/Dog/Human/Parrot/Chicken should return me 2


    Added after 24 minutes:


    Currently I solved it by iteratively checking if the parent is NULL. Is there a better way ?
    Last edited by mqt; 3rd October 2013 at 08:39.

  2. #2
    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: Level of an item QStandardItemModel for tree

    Following the parent chain is the only generic way.
    Of course, since you control the data of the model, you could store the depth for each entry using a custom role.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    mqt (3rd October 2013)

Similar Threads

  1. Replies: 2
    Last Post: 22nd August 2013, 10:50
  2. Replies: 2
    Last Post: 11th February 2012, 00:28
  3. QStandardItemModel, parent / child to form tree structure
    By Nightfox in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 17:01
  4. Crash while checking the index is of particular level of a tree
    By kapil sharma in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2008, 07:21
  5. Replies: 14
    Last Post: 9th November 2006, 08:35

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.