qtreewidget - contains items in a tree-like structure

Name

qtreewidget — contains items in a tree-like structure

Syntax

qtreewidget [parent]

Description

The qtreewidget command creates a QTreeWidget object. This is a widget containing so-called items. The items are arranged hierarchically in a tree-like structure. The QTreeWidget can contain thousands of items.

The QTreeWidget must have at least one column. Columns are added using the qsetheaderlabels command.

Items are created with the qtreewidgetitem command. This command returns the unique id for that item.

The commands for navigating in the tree structure are qparent, qtoplevelitem, qtoplevelitemcount, qchildcount, and qchild.

The current item can be set using the qsetcurrentitem.

Tree widget items have a type assigned during construction. A type describes the class or category to which the item belongs. The user is free to choose any type name. The type name is used when associating a right-click menu with a group of items, described below.

Items that have children will be displayed with a small marker at their left side. This feature can be set using the qsetrootisdecorated command.

QTreeWidget typically contain several right-click menus. There are two main classes of right-click menus: The background and item menu. The background menu is created using the qmenu command and is displayed when the user clicks on the listview background. The item menu is created with the qmenu command and displayed when the user right-clicks an item.

Each item may have a pixmap or text.

Arguments

parent

The parent widget.

Examples

Ex.1: QTreeWidget

  set w [qwidget]
  qsetwindowtitle QTreeWidget
  qsetattribute deleteonclose
  qvboxlayout {
  # Create tree and headers
  set tree [qtreewidget ]
  qsetheaderlabels { "File" "Path" }
  
  # Create a folder and two subitems.
  # Items need a type integer - we will let the folders have type '1'
  set folder [qtreewidgetitem Folder 1 ]
  
  # Assign icon in column 0
  set foldericon [qicon c:/folderclosed.xpm ]
  qseticon $folder 0 $foldericon
  
  # Create two items as child of folder
  # Item text is a list - one element for each column
  set child1 [qtreewidgetitem $folder { Child c:/image_data.xpm } 2]
  set child2 [qtreewidgetitem $folder { "Child 2" c:/structured_points.xpm } 2]

  # Assign icon to child items
  set image_icon [qicon c:/image_data.xpm]
  set grid_icon [qicon c:/structured_points.xpm]

  qseticon $child1 0 $image_icon
  qseticon $child2 0 $grid_icon


  }

  qshow $w

 

Example QTreeWidget show a QTreeWidget with two child items sharing a single parent item.

Ex.2: QTreeWidget with menus

  # Callback for "Hello" in tree menu
  proc show_hello {} {
  qinformation Hello "Hello there!"
  }

  set w [qwidget]
  qsetwindowtitle QTreeWidget
  qsetattribute deleteonclose
  qvboxlayout {
  # Create tree and headers
  set tree [qtreewidget ]
  qsetheaderlabels { "File" "Path" }
  
  # Create a folder and two subitems.
  # Items need a type integer - we will let the folders have type '1'
  set folder [qtreewidgetitem Folder 1 ]
  
  # Assign icon in column 0
  set foldericon [qicon c:/folderclosed.xpm ]
  qseticon $folder 0 $foldericon
  
  # Create two items as child of folder
  # Item text is a list - one element for each column
  set child1 [qtreewidgetitem $folder { Child c:/image_data.xpm } 2]
  set child2 [qtreewidgetitem $folder { "Child 2" c:/structured_points.xpm } 2]

  # Assign icon to child items
  set image_icon [qicon c:/image_data.xpm]
  set grid_icon [qicon c:/structured_points.xpm]

  qseticon $child1 0 $image_icon
  qseticon $child2 0 $grid_icon

  # Create right-click menu for widget background. 
  set treemenu [qmenu ]
  set helloaction [qaction Hello show_hello]
  qaddaction $treemenu $helloaction
  qsetmenu $tree $treemenu


  qseticon $child1 0 $image_icon
  qseticon $child2 0 $grid_icon

  # Create right-click menu for widget background. 
  set treemenu [qmenu ]
  set helloaction [qaction Hello show_hello]
  qaddaction $treemenu $helloaction
  qsetmenu $tree $treemenu 1
  
  # Create right click menu for Folders (type 1)
  set foldermenu [qmenu ]
  set folderaction [qaction "Hello Folder" "qinformation Folder {Hello folder}"]
  qaddaction $foldermenu $folderaction
  qsetmenu $tree $foldermenu 1
  
  }

  qshow $w

Example QTreeWidget with menus is similar to QTreeWidget but shows how to assign menus for the treewidget itself as well as for items.

Ex.3: Traversing Tree Items

  # Recursive procedure to print items and their children
  proc print_children { item } {
  puts "Itemtext is [qtext $item 0 ] [qtext $item 1], type is [qtype $item]"
  
  #Print children
  set childcount [qchildcount $item]
  for { set i 0 } { $i < $childcount } { incr i } {
  set childitem [qchild $item $i]
  print_children $childitem
  
  }
  
  }

  # Create toplevel window with layout
  set w [qwidget ]
  set layout [qvboxlayout $w]

  # Create tree and add to layout
  set tree [qtreewidget ]
  qaddwidget $layout $tree

  # Set column headers
  qsetheaderlabels $tree { Name Type }

  # Create some toplevel items
  set top1 [qtreewidgetitem $tree { House JPG } 1]
  set top2 [qtreewidgetitem $tree { River JPG } 1]

  # Create two children of 'top1'
  set top1_child1 [qtreewidgetitem $top1 { Garage PNG } 2]
  set top1_child2 [qtreewidgetitem $top1 { Lawn PNG } 2]

  # Print items recursively
  puts "Tree Items : "
  # Number of toplevel items (2)
  set topcount [qtoplevelitemcount $tree]
  for { set i 0 } { $i < $topcount } { incr i } {
  set item [qtoplevelitem $tree $i]
  print_children $item
  
  }

  qshow $w

Example Traversing Tree Items shows how to traverse a tree recursively.