Class overview   Alphabetical list   Annotated list   Header Files   Member List  

TreeWidget Class Reference


The TreeWidget class provides a scrollable tree view a la Windows Explorer. More...

#include <tree.h>

List of all member functions.

Public Members

Public Slots

Signals

Private Members

Private Slots


Detailed Description

The TreeWidget class provides a scrollable tree view a la Windows Explorer.

The TreeWidget class provides an area on the screen which contains a hierarchy of nodes. Each node consists of a text and an optional pixmap. Each node can be associated with a piece of data which can be requested later (see setNodeData()). The TreeWidget uses a pixmap buffer to ensure smooth updates. It comes with two scrollbars for x and y direction which become active automatically. Nodes in the tree can be highlighted by pressing the left mouse button. The right mouse button is used to pop up optional menus. The user can provide these menus in the form of QPopupMenu pointers.


Member Function Documentation

TreeWidget::TreeWidget(QWidget *parent, const char *name)

Construct a TreeWidget. The parent and name arguments are sent to the QWidget constructor.

TreeWidget::~TreeWidget()

Destroy the TreeWidget.

void TreeWidget::setCurrentNode(TreeNode *id)

Set the current node of the TreeWidget to the node identified by id. The new current node will be highlighted. If id is null, nothing will be selected.

TreeNode* TreeWidget::rootNode()

Returns a TreeNode pointer to the root node of the TreeWidget.

TreeNode* TreeWidget::currentNode()

Returns the TreeNode pointer of the currently selected node of the TreeWidget. If no node is selected, null is returned.

NodeList* TreeWidget::nodes()

Returns a list of TreeNode pointers of all nodes in the tree. Example:

    NodeList *list = myTree->nodes();
    NodeListIt it(*list);
    TreeNode *curr;
    for(; it.current(); ++it)
    {
      ....
    }
    delete list;
  

Warning: Delete the list as soon as you are done. It is allocated specifically for you. If you don't delete it, you'l have a memory leak on your hands!

TreeNode* TreeWidget::addNode(const char *txt, QPixmap *pic, void *data, TreeNode *parent)

Adds a new node to the tree using txt as the node text and pic as the pixmap. The data parameter is an opaque container which is cast to (void *). This way you can store proprietary data along with the new node. You can later use the data() member function of the TreeNode to retrieve this data. The parent parameter is to locate the parent for the new tree node. If parent is 0, the new node will be used as the root node of the tree. Note that you can only have one root node. If a root node is present, you will need to delete it first using rmNode().

A pointer to the newly created TreeNode is returned. This pointer can be used as a reference later if you wish to retrieve information about the node.

Warning: The TreeWidget retains ownership of all the TreeNode-s in the tree. Do not ever attempt to delete a TreeNode!

void TreeWidget::addNode(TreeNode *node, TreeNode *parent)

Adds a new node to the tree. The node should be created by the user. It is placed in the tree as a child of the treenode pointed to by parent. The TreeWidget assumes ownership of the TreeNode after it has been added to the tree in this way. However, it does not own the data and pixmap members of the TreeNode. Freeing the storage related to those is up to the user.

void TreeWidget::rmNode(TreeNode *target)

Remove the tree node identified by target. The corresponding node and all it's children will be deleted.

Warning: If the TreeNode contains a pixmap or data, neither of these will be deleted! Whoever create the node with addNode() retains ownership of this memory and has the reponsibility to delete it.

void TreeWidget::clear()

Clear the whole TreeWidget. All nodes in the tree are deleted, including the root node.

Warning: If any of the TreeNodes contain pixmaps or data, neither of these will be deleted! Whoever create the node with addNode() retains ownership of this memory and has the reponsibility to delete it.

void TreeWidget::setNodePopup(QPopupMenu *menu)

Specifies the popup menu that should be shown when the user right clicks on a selected node. By default, nothing hapens in this case. You can use this function to determine the operations that can be done on a node Example:

    MyClass::MyClass(QWidget *parent, const char *name)
    : QWidget(parent, name)
    {
      TreeWidget *tree = new TreeWidget(this);
      tree->setGeometry(...);
      ....
      QPopupMenu *nodePopup = new QPopupMenu(0);
      nodePopup->insertItem("&Delete", this, SLOT(deleteNode()) );
      nodePopup->insertItem("&Copy", this, SLOT(copyNode()) );
      nodePopup->insertItem("&Edit", this, SLOT(editNode()) );
      ....
      tree->setNodePopup(nodePopup);
    }
  

The deleteNode() SLOT could look like this:

    void MyClass::deleteNode(void)
    {
      ....
      TreeNode *curr = tree->currentNode();
      tree->rmNode(curr);
      ....
    }
  

void TreeWidget::setBackPopup(QPopupMenu *menu)

Specifies the popup menu that should be shown when the user right clicks on a the background of the TreeWidget. By default, nothing hapens in this case. You can use this function to determine the operations that are available for the whole tree object. Example:

    MyClass::MyClass(QWidget *parent, const char *name)
    : QWidget(parent, name)
    {
      TreeWidget *tree = new TreeWidget(this);
      tree->setGeometry(...);
      ....
      QPopupMenu *backPopup = new QPopupMenu(0);
      backPopup->insertItem("&Clear", this, SLOT(clearTree()) );
      backPopup->insertItem("&Quit", this, SLOT(quit()) );
      ....
      tree->setBackPopup(backPopup);
    }
  

void TreeWidget::update() [slot]

Force an update of the information in the tree. Call this function if you have updated the content of any of the TreeNode-s. If you don't call this function then you will have to wait until the next forced repaint.

void TreeWidget::highlighted() [signal]

This signal is emitted when a new item has been highlighted by selecting it with the left mouse button.

void TreeWidget::selected() [signal]

This signal is emitted when a new item is selected by double-clicking a tree node with the left mouse button.

void TreeWidget::nothingSelected() [signal]

This signal is emitted when all selections are cancelled by clicking the left mouse button in the background of the TreeWidget.

void TreeWidget::mousePressEvent(QMouseEvent *ev) [private]

Handles mouse click events. If the left mouse button is pressed inside the area used to draw a node, that node is selected. If the left mouse button is pressed in the background area which is not used to draw nodes on, all nodes are deselected and nothingSelected() is emitted. If the right mouse button is pressed on a node, the nodePopup menu is shown. If it is pressed in the background area, the backPopup menu is shown.

void TreeWidget::mouseDoubleClickEvent(QMouseEvent *) [private]

Handles mouse doubleclick events

void TreeWidget::paintEvent(QPaintEvent *ev) [private]

Paints the buffer containing the pixmap pixture on the screen.

void TreeWidget::resizeEvent(QResizeEvent *) [private]

Handles resize events. Calls update() to ensure that the background buffer pixmap is appropriately resized and updated. This function also updates the range and value of the x- and y scrollbars

void TreeWidget::draw() [private]

Paints the tree hierarchy into the background buffer.

Warning: This function \ul does not call update(). The content of the background buffer is not drawn on the screen!

void TreeWidget::doXBar(int val) [private, slot]

Handle movement of the x-scrollbar. Scroll the treewidget left and right.

void TreeWidget::doYBar(int val) [private, slot]

Handle movement of the y-scrollbar. Scroll the treewidget up and down.


Member Variable Documentation

QScrollBar* TreeWidget::_xBar [private]

The horizontal scrollbar

QScrollBar* TreeWidget::_yBar [private]

The vertical scrollbar

TreeNode* TreeWidget::_rootNode [private]

The root node of the tree

TreeNode* TreeWidget::_currNode [private]

The currently selected node

QList <TreeNode> TreeWidget::_nodeList [private]

A list of nodes in the tree

QPixmap* TreeWidget::_canvas [private]

The background buffer used for flicker-free updates

QPoint TreeWidget::_offset [private]

The offset for drawing the (clipped) tree

QPopupMenu* TreeWidget::_nodePopup [private]

The popup menu for the tree nodes

QPopupMenu* TreeWidget::_backPopup [private]

The popup menu for the tree background


This file was generated from the following files:
Generated at 14:38, 1998/04/17 for Ebuilder by doxygen  written by Dimitri van Heesch, © 1997-1998