Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt5 qtreewidgetitem enable drop drag

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

IDRole = QtCore.Qt.UserRole + 1000

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()

        self.tree = QtWidgets.QTreeWidget(columnCount=1,
            dragDropMode=QtWidgets.QAbstractItemView.DragDrop,
            dragEnabled=True)
        self.tree.hideColumn(1)

        self.tree.itemDoubleClicked.connect(self.rename)
        self.tree.itemChanged.connect(self.postRename)

        # add treewidgetitems
        data=[['Folder 1', '1'],
              ['Folder 2', '2'],
              ['Folder 3', '3']
              ]
        for d in data:
            text, itemid = d
            item = QtWidgets.QTreeWidgetItem([text])
            item.setData(0, IDRole, itemid)
            self.tree.addTopLevelItem(item)

        frame = QtWidgets.QWidget()
        self.setCentralWidget(frame)
        hl = QtWidgets.QVBoxLayout(frame)
        hl.addWidget(self.tree)

    @QtCore.pyqtSlot()
    def rename(self):
        item=self.tree.selectedItems()
        if item:
            item=item[0]
            item.setFlags(item.flags() | Qt.ItemIsEditable)
            self.tree.scrollToItem(item)
            self.tree.editItem(item)

    @QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem, int)
    def postRename(self, item, column):
        print('postRename: column counts', item.columnCount())
        text = item.text(0)
        itemid = item.data(0, IDRole)
        print('postRename: item text=',text, 'item id', itemid)

if __name__ == "__main__":
     app = QtWidgets.QApplication(sys.argv)
     form = MainWindow()
     form.show()
     sys.exit(app.exec_())
Comment

PREVIOUS NEXT
Code Example
Python :: matplotlib pyplot comment on plot 
Python :: pyspark dataframe to parquet 
Python :: python docx extract image 
Python :: flask decoding base 64 image 
Python :: string print in pattern in python 
Python :: remove multiindex pandas 
Python :: button tkinter 
Python :: Python program to draw hexagon 
Python :: PhoneNumberField django forms 
Python :: extract email address using expression in django 
Python :: pygityb 
Python :: pd.read_csv 
Python :: numpy expand_dims 
Python :: with open as file python 
Python :: pandas description of dataframe 
Python :: print python float precision 
Python :: same elements of two sets in python 
Python :: pandas swapaxes example 
Python :: python absolute path from projectr 
Python :: convert all colnames of dataframe to upper 
Python :: Math Module tan() Function in python 
Python :: pandas return row 
Python :: do not show figure matplotlib 
Python :: append object python 
Python :: python string contains 
Python :: python dataclass 
Python :: python youtube downloader 
Python :: pandas -inf and inf to 0 
Python :: python pd.Timestamp add days 
Python :: pandas replace last cell 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =