Results 1 to 9 of 9

Thread: Hide Parent and show only children in QTreeView

  1. #1
    Join Date
    May 2008
    Posts
    12
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Hide Parent and show only children in QTreeView

    Hi All,

    I am stuck with a problem, I was wondering is there any way to just show children in a Qtreeview not it's parent.
    For instance.

    If this is the tree

    Qt Code:
    1. AAA--
    2. |
    3. A _
    4. |
    5. a1
    6. |
    7. a2
    8. |
    9. a3
    10. |
    11. a4
    To copy to clipboard, switch view to plain text mode 

    I want only this to be shown, that is without Root node (AAA in this case)

    Qt Code:
    1. A _
    2. |
    3. a1
    4. |
    5. a2
    6. |
    7. a3
    8. |
    9. a4
    To copy to clipboard, switch view to plain text mode 


    I searched a lot in net, but without success. If I hide the root row, I am not able to see it's children,I think there should be some way to do that .


    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Hide Parent and show only children in QTreeView

    QTreeView::setRootIndex() looks good to me.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hide Parent and show only children in QTreeView

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2008
    Posts
    12
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hide Parent and show only children in QTreeView

    Thanks you for your reply...
    I am doing the same thing in my code but has no effect...

    MyModel* model = new MyModel( ui->treeView ); //MyModel inherits QAbstractItemModel
    ui->treeView->setModel(model);
    ui->treeView->setRootIndex(model->index(2,0,QModelIndex()));

    I want to make third row my parent.


    modelView.png

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hide Parent and show only children in QTreeView

    It has no effect because your model has only one row in its top-most level of the hierarchy.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2008
    Posts
    12
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hide Parent and show only children in QTreeView

    Thanks wysota,
    when I changed

    ui->treeView->setRootIndex(model->index(2,0,QModelIndex()));

    to

    ui->treeView->setRootIndex(model->index(0,0,QModelIndex()));

    First root become invisible, but is there anyway to make second child of root, main root ?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hide Parent and show only children in QTreeView

    You can make any index the root index if you pass it to setRootIndex().
    Qt Code:
    1. QModelIndex grandparentIndex = model->index(0,0);
    2. QModelIndex parentIndex = model->index(0,0, grandparentIndex);
    3. QModelIndex idx = model->index(2, 0, parentIndex);
    4. ui->treeView->setRootIndex(idx);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Hide Parent and show only children in QTreeView

    There is no second child of the root in your example... that's the point. The third row in your display is the first child of the first child of the topmost item.


    Edit: Yeah, what wysota said.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hide Parent and show only children in QTreeView

    Yeah, of course my code won't work with this exact model because there is no third child of the first child of the first child of the model. But I hope OP gets the picture now.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 6
    Last Post: 22nd December 2011, 21:03
  2. Replies: 3
    Last Post: 19th March 2011, 15:01
  3. Replies: 0
    Last Post: 14th April 2010, 15:03
  4. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 06:49
  5. Replies: 3
    Last Post: 29th May 2008, 13:50

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.