Understanding QTreeWidgetItem::childIndicatorPolicy() in Qt Widgets


Purpose

  • This icon typically indicates whether the item has child items.
  • Controls the display of an indicator icon next to a QTreeWidgetItem in a QTreeWidget.

Function

  • This policy determines when and how the indicator icon is displayed.
  • Returns the current ChildIndicatorPolicy for the item.

Available Policies (Qt Widgets 6.7.1)

  • DontShowIndicator: Never display the indicator.
  • ShowIndicatorWhenChildless: Display the indicator only if the item does not have children (often used for expandable items).
  • ShowIndicator: Always display the indicator, regardless of whether the item has children.

Setting the Policy

  • Use the setChildIndicatorPolicy() function on the QTreeWidgetItem object:
#include <QtWidgets>

// ...

QTreeWidgetItem *item = new QTreeWidgetItem();
item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicatorWhenChildless);

Example

Consider a tree structure where some top-level items have child items, while others don't. You might want to:

  • Not show any indicator for leaf nodes (items without children).
  • Display a plus sign (+) next to items that can be expanded to reveal children (using ShowIndicatorWhenChildless).
  • You can combine this functionality with other aspects of tree item management, such as setting flags for expandability (setExpanded() and isExpanded()) or dynamically adding/removing child items.
  • The indicator icon's appearance is controlled by the tree widget's stylesheet or custom delegate.


#include <QtWidgets>

class MyTreeWidget : public QTreeWidget {
    Q_OBJECT

public:
    MyTreeWidget(QWidget *parent = nullptr) : QTreeWidget(parent) {
        setColumnCount(1);
        setHeaderLabel("Items");

        // Create some sample items
        QTreeWidgetItem *item1 = new QTreeWidgetItem(this, QStringList({"Parent 1"}));
        item1->addChild(new QTreeWidgetItem(QStringList({"Child 1.1"})));
        item1->addChild(new QTreeWidgetItem(QStringList({"Child 1.2"})));

        QTreeWidgetItem *item2 = new QTreeWidgetItem(this, QStringList({"Parent 2"}));
        item2->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicatorWhenChildless);

        QTreeWidgetItem *item3 = new QTreeWidgetItem(this, QStringList({"Leaf Node"}));
        item3->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MyTreeWidget tree;
    tree.show();
    return app.exec();
}
    • Inherits from QTreeWidget.
    • Creates a single column with a header label "Items".
  1. Sample Items

    • Creates three top-level items:
      • item1: Has two child items (Parent 1 with children).
      • item2: Has the ShowIndicatorWhenChildless policy set (initially empty).
      • item3: Has the DontShowIndicator policy set (leaf node).
  2. main Function

    • Creates a QApplication object.
    • Creates an instance of MyTreeWidget and shows it.

Expected Behavior

  • The tree will display:
    • Parent 1 with a plus sign (+) because it has child items.
    • Parent 2 with a minus sign (-) because it's currently empty but can be expanded.
    • Leaf Node without any indicator as it's a leaf node.


Custom Delegate

  • Set the custom delegate for your tree widget using setItemDelegate().
  • Override the paint() function to draw your own indicator based on the item's properties (e.g., number of children, expanded state).
  • Create a custom delegate class that inherits from QItemDelegate.

This approach offers more flexibility in customizing the indicator's appearance and behavior. However, it requires more code to implement the custom painting logic.

Subclassing QTreeWidgetItem

  • Connect the tree widget's itemClicked() signal to a slot that checks the custom data for the clicked item and updates the indicator (e.g., using stylesheets) based on the child presence.
  • Override a virtual function like data() to check for child items and return a custom value indicating the presence of children.
  • Create a subclass of QTreeWidgetItem.

This approach provides some control over the indicator but involves more code compared to setting the policy directly. It also requires handling indicator updates manually on item interaction.

Stylesheets

  • Use selectors targeting specific item states (e.g., :!has-children) to hide or modify the default indicator appearance.
  • Apply stylesheets to your tree widget using setStyleSheet().

This approach is simpler to implement but offers less flexibility in terms of indicator behavior compared to custom delegates. It also relies on the underlying implementation of the tree widget's indicators.

  • Stylesheets can be a quick way to modify the default indicator appearance without much code, but they offer limited behavioral control.
  • Subclassing QTreeWidgetItem might be suitable if you have additional logic tied to child presence, but it involves more work.
  • If you need more granular control over the indicator's appearance or behavior, consider using a custom delegate.
  • For basic control over the indicator's presence based on child items, QTreeWidgetItem::childIndicatorPolicy() is the recommended and easiest option.