How to detect QTreeWidgetItem is double-clicked in PySide

GOAL

Today’s goal is to execute a function when QTreeWidgetItem is double clicked.

Environment

Windows 10
Python 3.8.7
PySide2 (5.15.2)

Method

First I created a sample QTreeWidget as below.

import sys
from PySide2.QtWidgets import *


class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self._generate_ui()
        self._init_tree_widget()

    def _generate_ui(self):
        main_widget = QWidget()
        main_layout = QVBoxLayout()
        main_widget.setLayout(main_layout)
        self.setCentralWidget(main_widget)
        self.tree_widget = QTreeWidget()
        self.tree_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.tree_widget.setSelectionBehavior(QAbstractItemView.SelectItems)
        main_layout.addWidget(self.tree_widget)

    def _init_tree_widget(self):
        headers = ["header1", "header2"]
        self.tree_widget.setHeaderLabels(headers)
        tree_widget_item1 = QTreeWidgetItem(["group1"])
        tree_widget_item1.addChild(QTreeWidgetItem(["item1_1", "item1_2"]))
        self.tree_widget.addTopLevelItem(tree_widget_item1)
        tree_widget_item2 = QTreeWidgetItem(["group2"])
        self.tree_widget.addTopLevelItem(tree_widget_item2)
        tree_widget_item2.addChild(QTreeWidgetItem(["item2_1", "item2_2"]))
        tree_widget_item2.addChild(QTreeWidgetItem(["item3_1", "item3_2"]))


def launch():
    app = QApplication.instance()
    if not app:
        app = QApplication(sys.argv)
    widget = MyMainWindow()
    widget.show()
    app.exec_()


launch()

1. How to detect that item is double clicked

Connect the function to QTreeWidget.itemDoubleClicked signal.

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        # omitted
    def _generate_ui(self):
        main_widget = QWidget()
        main_layout = QVBoxLayout()
        main_widget.setLayout(main_layout)
        self.setCentralWidget(main_widget)
        self.tree_widget = QTreeWidget()
        self.tree_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.tree_widget.setSelectionBehavior(QAbstractItemView.SelectItems)
        self.tree_widget.itemDoubleClicked.connect(self._doubleClicked) # connect function to the signal itemDoubleClicked
        main_layout.addWidget(self.tree_widget)

    def _init_tree_widget(self):
        # omitted
        
    def _doubleClicked(self): # this function is executed when an item is double clicked
        print("double clicked")

2. How to get double clicked item

Use QTreeWidget.currentItem(), QTreeWidget.currentColumn() or QAbstractItemView.currentIndex().

    def _doubleClicked(self):
        column = self.tree_widget.currentColumn()
        text = self.tree_widget.currentItem().text(column)
        print("double clicked item is " + text)

# output when item1_2 clicked => double clicked item is item1_2
# output when group1 clicked => double clicked item is grounp1