PySide6.QtWidgets.QTreeWidget¶
- class QTreeWidget¶
- The - QTreeWidgetclass provides a tree view that uses a predefined tree model. More…- Synopsis¶- Properties¶- columnCountᅟ- The number of columns displayed in the tree widget
- topLevelItemCountᅟ- The number of top-level items
 - Methods¶- def - __init__()
- def - columnCount()
- def - currentColumn()
- def - currentItem()
- def - editItem()
- def - findItems()
- def - headerItem()
- def - indexFromItem()
- def - itemAbove()
- def - itemAt()
- def - itemBelow()
- def - itemFromIndex()
- def - itemWidget()
- def - selectedItems()
- def - setColumnCount()
- def - setCurrentItem()
- def - setHeaderItem()
- def - setHeaderLabel()
- def - setItemWidget()
- def - sortColumn()
- def - sortItems()
- def - topLevelItem()
- def - visualItemRect()
 - Virtual methods¶- def - dropMimeData()
- def - mimeData()
- def - mimeTypes()
 - Slots¶- def - clear()
- def - collapseItem()
- def - expandItem()
- def - scrollToItem()
 - Signals¶
- def - itemActivated()
- def - itemChanged()
- def - itemClicked()
- def - itemCollapsed()
- def - itemEntered()
- def - itemExpanded()
- def - itemPressed()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors.   - The - QTreeWidgetclass is a convenience class that provides a standard tree widget with a classic item-based interface similar to that used by the- QListViewclass in Qt 3. This class is based on Qt’s Model/View architecture and uses a default model to hold items, each of which is a- QTreeWidgetItem.- Developers who do not need the flexibility of the Model/View framework can use this class to create simple hierarchical lists very easily. A more flexible approach involves combining a - QTreeViewwith a standard item model. This allows the storage of data to be separated from its representation.- In its simplest form, a tree widget can be constructed in the following way: - treeWidget = QTreeWidget() treeWidget.setColumnCount(1) *> = QList<QTreeWidgetItem() for i in range(0, 10): items.append(QTreeWidgetItem(QTreeWidget(None), QStringList(QString("item: %1").arg(i)))) treeWidget.insertTopLevelItems(0, items) - Before items can be added to the tree widget, the number of columns must be set with - setColumnCount(). This allows each item to have one or more labels or other decorations. The number of columns in use can be found with the- columnCount()function.- The tree can have a header that contains a section for each column in the widget. It is easiest to set up the labels for each section by supplying a list of strings with - setHeaderLabels(), but a custom header can be constructed with a- QTreeWidgetItemand inserted into the tree with the- setHeaderItem()function.- The items in the tree can be sorted by column according to a predefined sort order. If sorting is enabled, the user can sort the items by clicking on a column header. Sorting can be enabled or disabled by calling - setSortingEnabled(). The- isSortingEnabled()function indicates whether sorting is enabled.- Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property columnCountᅟ: int¶
 - This property holds the number of columns displayed in the tree widget. - By default, this property has a value of 1. - Access functions:
 - property topLevelItemCountᅟ: int¶
 - This property holds the number of top-level items. - By default, this property has a value of 0. - See also - Access functions:
 - Constructs a tree widget with the given - parent.- addTopLevelItem(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - Appends the - itemas a top-level item in the widget.- See also - addTopLevelItems(items)¶
- Parameters:
- items – .list of QTreeWidgetItem 
 
 - Appends the list of - itemsas a top-level items in the widget.- See also - clear()¶
 - Clears the tree widget by removing all of its items and selections. - Note - Since each item is removed from the tree widget before being deleted, the return value of - treeWidget()will be invalid when called from an item’s destructor.- closePersistentEditor(item[, column=0])¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - Closes the persistent editor for the - itemin the given- column.- This function has no effect if no persistent editor is open for this combination of item and column. - collapseItem(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - Closes the - item. This causes the tree containing the item’s children to be collapsed.- See also - columnCount()¶
- Return type:
- int 
 - See also 
 - Getter of property - columnCountᅟ.- currentColumn()¶
- Return type:
- int 
 
 - Returns the current column in the tree widget. - See also - currentItem()¶
- Return type:
 
 - Returns the current item in the tree widget. - See also - currentItemChanged(current, previous)¶
- Parameters:
- current – - QTreeWidgetItem
- previous – - QTreeWidgetItem
 
 
 - This signal is emitted when the current item changes. The current item is specified by - current, and this replaces the- previouscurrent item.- See also - dropMimeData(parent, index, data, action)¶
- Parameters:
- parent – - QTreeWidgetItem
- index – int 
- data – - QMimeData
- action – - DropAction
 
- Return type:
- bool 
 
 - Handles the - datasupplied by a drag and drop operation that ended with the given- actionin the- indexin the given- parentitem.- The default implementation returns - trueif the drop was successfully handled by decoding the mime data and inserting it into the model; otherwise it returns- false.- See also - editItem(item[, column=0])¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - Starts editing the - itemin the given- columnif it is editable.- expandItem(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - Expands the - item. This causes the tree containing the item’s children to be expanded.- findItems(text, flags[, column=0])¶
- Parameters:
- text – str 
- flags – Combination of - MatchFlag
- column – int 
 
- Return type:
- .list of QTreeWidgetItem 
 
 - Returns a list of items that match the given - text, using the given- flags, in the given- column.- headerItem()¶
- Return type:
 
 - Returns the item used for the tree widget’s header. - See also - indexFromItem(item[, column=0])¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
- Return type:
 
 - Returns the QModelIndex associated with the given - itemin the given- column.- Note - In Qt versions prior to 5.7, this function took a non- - const- item.- See also - indexOfTopLevelItem(item)¶
- Parameters:
- item – - QTreeWidgetItem
- Return type:
- int 
 
 - Returns the index of the given top-level - item, or -1 if the item cannot be found.- See also - insertTopLevelItem(index, item)¶
- Parameters:
- index – int 
- item – - QTreeWidgetItem
 
 
 - Inserts the - itemat- indexin the top level in the view.- If the item has already been inserted somewhere else it won’t be inserted. - See also - insertTopLevelItems(index, items)¶
- Parameters:
- index – int 
- items – .list of QTreeWidgetItem 
 
 
 - Inserts the list of - itemsat- indexin the top level in the view.- Items that have already been inserted somewhere else won’t be inserted. - See also - invisibleRootItem()¶
- Return type:
 
 - Returns the tree widget’s invisible root item. - The invisible root item provides access to the tree widget’s top-level items through the - QTreeWidgetItemAPI, making it possible to write functions that can treat top-level items and their children in a uniform way; for example, recursive functions.- isPersistentEditorOpen(item[, column=0])¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
- Return type:
- bool 
 
 - Returns whether a persistent editor is open for item - itemin column- column.- itemAbove(item)¶
- Parameters:
- item – - QTreeWidgetItem
- Return type:
 
 - Returns the item above the given - item.- itemActivated(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - This signal is emitted when the user activates an item by single- or double-clicking (depending on the platform, i.e. on the - SH_ItemView_ActivateItemOnSingleClickstyle hint) or pressing a special key (e.g., Enter).- The specified - itemis the item that was clicked, or- Noneif no item was clicked. The- columnis the item’s column that was clicked, or -1 if no item was clicked.- Returns a pointer to the item at the coordinates - p. The coordinates are relative to the tree widget’s- viewport().- See also - itemAt(x, y)
- Parameters:
- x – int 
- y – int 
 
- Return type:
 
 - This is an overloaded function. - Returns a pointer to the item at the coordinates ( - x,- y). The coordinates are relative to the tree widget’s- viewport().- itemBelow(item)¶
- Parameters:
- item – - QTreeWidgetItem
- Return type:
 
 - Returns the item visually below the given - item.- itemChanged(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - This signal is emitted when the contents of the - columnin the specified- itemchanges.- itemClicked(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - This signal is emitted when the user clicks inside the widget. - The specified - itemis the item that was clicked. The- columnis the item’s column that was clicked. If no item was clicked, no signal will be emitted.- itemCollapsed(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - This signal is emitted when the specified - itemis collapsed so that none of its children are displayed.- Note - This signal will not be emitted if an item changes its state when - collapseAll()is invoked.- See also - itemDoubleClicked(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - This signal is emitted when the user double clicks inside the widget. - The specified - itemis the item that was clicked, or- Noneif no item was clicked. The- columnis the item’s column that was clicked. If no item was double clicked, no signal will be emitted.- itemEntered(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - This signal is emitted when the mouse cursor enters an - itemover the specified- column.- QTreeWidgetmouse tracking needs to be enabled for this feature to work.- itemExpanded(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - This signal is emitted when the specified - itemis expanded so that all of its children are displayed.- See also - itemFromIndex(index)¶
- Parameters:
- index – - QModelIndex
- Return type:
 
 - Returns a pointer to the - QTreeWidgetItemassociated with the given- index.- See also - itemPressed(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - This signal is emitted when the user presses a mouse button inside the widget. - The specified - itemis the item that was clicked, or- Noneif no item was clicked. The- columnis the item’s column that was clicked, or -1 if no item was clicked.- itemSelectionChanged()¶
 - This signal is emitted when the selection changes in the tree widget. The current selection can be found with - selectedItems().- itemWidget(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
- Return type:
 
 - Returns the widget displayed in the cell specified by - itemand the given- column.- See also - Returns an object that contains a serialized description of the specified - items. The format used to describe the items is obtained from the- mimeTypes()function.- If the list of items is empty, - Noneis returned rather than a serialized empty list.- mimeTypes()¶
- Return type:
- list of strings 
 
 - Returns a list of MIME types that can be used to describe a list of treewidget items. - See also - openPersistentEditor(item[, column=0])¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - Opens a persistent editor for the - itemin the given- column.- removeItemWidget(item, column)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - Removes the widget set in the given - itemin the given- column.- See also - scrollToItem(item[, hint=QAbstractItemView.ScrollHint.EnsureVisible])¶
- Parameters:
- item – - QTreeWidgetItem
- hint – - ScrollHint
 
 
 - Ensures that the - itemis visible, scrolling the view if necessary using the specified- hint.- See also - selectedItems()¶
- Return type:
- .list of QTreeWidgetItem 
 
 - Returns a list of all selected non-hidden items. - See also - setColumnCount(columns)¶
- Parameters:
- columns – int 
 - See also 
 - Setter of property - columnCountᅟ.- setCurrentItem(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - Sets the current - itemin the tree widget.- Unless the selection mode is - NoSelection, the item is also selected.- See also - setCurrentItem(item, column)
- Parameters:
- item – - QTreeWidgetItem
- column – int 
 
 
 - Sets the current - itemin the tree widget and the current column to- column.- See also - setCurrentItem(item, column, command)
- Parameters:
- item – - QTreeWidgetItem
- column – int 
- command – Combination of - SelectionFlag
 
 
 - Sets the current - itemin the tree widget and the current column to- column, using the given- command.- See also - setHeaderItem(item)¶
- Parameters:
- item – - QTreeWidgetItem
 
 - Sets the header - itemfor the tree widget. The label for each column in the header is supplied by the corresponding label in the item.- The tree widget takes ownership of the item. - See also - setHeaderLabel(label)¶
- Parameters:
- label – str 
 
 - Same as - setHeaderLabels(QStringList(- label)).- setHeaderLabels(labels)¶
- Parameters:
- labels – list of strings 
 
 - Adds a column in the header for each item in the - labelslist, and sets the label for each column.- Note that setHeaderLabels() won’t remove existing columns. - See also - setItemWidget(item, column, widget)¶
- Parameters:
- item – - QTreeWidgetItem
- column – int 
- widget – - QWidget
 
 
 - Sets the given - widgetto be displayed in the cell specified by the given- itemand- column.- The given - widget's- autoFillBackgroundproperty must be set to true, otherwise the widget’s background will be transparent, showing both the model data and the tree widget item.- This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use - QTreeViewand subclass- QStyledItemDelegateinstead.- This function cannot be called before the item hierarchy has been set up, i.e., the - QTreeWidgetItemthat will hold- widgetmust have been added to the view before- widgetis set.- sortColumn()¶
- Return type:
- int 
 
 - Returns the column used to sort the contents of the widget. - See also - Sorts the items in the widget in the specified - orderby the values in the given- column.- See also - supportedDropActions()¶
- Return type:
- Combination of - DropAction
 
 - Returns the drop actions supported by this view. - See also - DropActions- takeTopLevelItem(index)¶
- Parameters:
- index – int 
- Return type:
 
 - Removes the top-level item at the given - indexin the tree and returns it, otherwise returns- None;- topLevelItem(index)¶
- Parameters:
- index – int 
- Return type:
 
 - Returns the top level item at the given - index, or- Noneif the item does not exist.- See also - topLevelItemCount()¶
- Return type:
- int 
 
 - Getter of property - topLevelItemCountᅟ.- visualItemRect(item)¶
- Parameters:
- item – - QTreeWidgetItem
- Return type:
 
 - Returns the rectangle on the viewport occupied by the item at - item.- See also