Results 1 to 2 of 2

Thread: QTreewidget : Select only one root node

  1. #1
    Join Date
    Dec 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreewidget : Select only one root node

    I have a tree structure shown below implemented within a QTreeWidget

    NodeA

    ->LeafA1

    ->LeafA2

    NodeB

    ->LeafB1

    ->LeafB2

    The restriction on selection are

    Only root nodes are selectable(code snippet below)

    Only one root node is selectable - Can readers let me know how this can be done?
    Qt Code:
    1. //Only select Root nodes
    2. if ( item->parent())
    3. {
    4. item->setCheckState(0, Qt::Unchecked);
    5. return;
    6. }
    7. //Set state for all leaf nodes
    8. Qt::CheckState state = item->checkState(0);
    9. QList<QTreeWidgetItem*>children = item->takeChildren();
    10. foreach(QTreeWidgetItem* child, children)
    11. {
    12. child->setCheckState(0, state);
    13. item->addChild(child);
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QTreewidget : Select only one root node

    Set the selection mode to SingleSelection and set the ItemIsSelectable flag for top level items only. Clear that flag for the remaining items.

    Your code snippet had nothing to do with items being selectable. It is related to making items checkable.
    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.


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

    Kryzon (5th March 2015)

Similar Threads

  1. Need to remove root node from QTreeView
    By tescik in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2014, 10:54
  2. QTreeWidget set invisible root node?
    By Braf in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2013, 03:03
  3. How to create root node inQDirModel
    By deepak in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2012, 08:03
  4. QTreeWidget: How to display the root item?
    By Johannes in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2011, 11:39
  5. customize the root node of a treeview
    By billconan in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 09:06

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.