Check out Kite (free AI Coding Assistant) → Link



Buy Me a Coffee? Your support is much appreciated!

PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn

Source Code: 

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QTreeWidget, QTreeWidgetItem, \
                            QHBoxLayout, QVBoxLayout, QItemDelegate
from PyQt6.QtCore import Qt, QRegularExpression #QRegExp
from PyQt6.QtGui import QRegularExpressionValidator

class TreeWidgetDelegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        editor = QLineEdit(parent=parent)
        pattern = '[A-z0-9]+'
        reg = QRegularExpression(pattern)
        reg_validator = QRegularExpressionValidator(reg)
        editor.setValidator(reg_validator)
        return editor

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.window_width, self.window_height = 700, 500
        self.resize(self.window_width, self.window_height)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.tree = QTreeWidget()
        self.tree.itemDoubleClicked.connect(self.rename_value)
        self.layout.addWidget(self.tree)

        for i in range(5):
            item = QTreeWidgetItem([str(i)*3])
            self.tree.addTopLevelItem(item)

        delegate = TreeWidgetDelegate()
        self.tree.setItemDelegate(delegate)

    def rename_value(self):
        item = self.tree.selectedItems()
        if item:
            item = item[0]
            item.setFlags(item.flags() | Qt.ItemFlags.ItemIsEditable)


if __name__ == '__main__':
    # don't auto scale when drag app to a different monitor.
    # QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    
    app = QApplication(sys.argv)
    app.setStyleSheet('''
        QWidget {
            font-size: 17px;
        }
    ''')
    
    myApp = MyApp()
    myApp.show()

    try:
        sys.exit(app.exec())
    except SystemExit:
        print('Closing Window...')