如何使用 PYQT5 select 和编辑 QTreeView 中新创建的文件夹

How to select and edit newly created folder in QTreeView using PYQT5

我正在尝试弄清楚如何 select 和编辑新创建的文件夹。下面是一些演示它的代码:

import os
import sys
from PyQt5.QtWidgets import (QApplication,
                             QMainWindow,
                             QLabel,
                             QLineEdit,
                             QPushButton,
                             QShortcut,
                             QFileSystemModel,
                             QTreeView,
                             QWidget,
                             QVBoxLayout,
                             QHBoxLayout,
                             QLayout,
                             QMenu,
                             QPlainTextEdit,
                             QSizePolicy,
                             QMessageBox,
                             QAbstractItemView)
from PyQt5.QtCore import QSize, Qt, QRect
from PyQt5.QtGui import QKeySequence

class FSView(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setFixedSize(size.width()*1/4, size.height()*0.85)

        self.model = QFileSystemModel()
        self.model.setRootPath('')

        self.model.setReadOnly(False)
        self.tree = QTreeView()
        self.tree.setContextMenuPolicy(Qt.CustomContextMenu)
        self.tree.customContextMenuRequested.connect(self.openMenu)
        self.tree.setModel(self.model)

        self.tree.setAnimated(False)
        self.tree.setIndentation(20)
        self.tree.setDragDropMode(QAbstractItemView.InternalMove)

        windowLayout = QVBoxLayout()
        windowLayout.addWidget(self.tree)
        self.setLayout(windowLayout)

    def openMenu(self, position):
        menu = QMenu()
        menu.addAction('New folder', self.NewF)
        menu.exec_(self.tree.viewport().mapToGlobal(position))

    def NewF(self):
        d = str(self.model.filePath(self.tree.currentIndex())) + '/New folder'
        if not os.path.exists(d):
            os.mkdir(d)
#        correctIndex = self.tree.currentIndex() + 1 #not working
#        self.tree.edit(correctIndex)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    screen = app.primaryScreen()
    size = screen.size()

    ex = FSView()
    ex.show()
    sys.exit(app.exec_())

创建新文件夹后,我希望它同时处于 selected 和编辑模式(即:self.tree.edit(correctIndex))。

我检查了一些帖子 (here),但仍然没有找到正确的索引。

感谢您的建议。

使用您的代码,您必须首先使用 QTreeView 的 index() method of QFileSystemModel passing it the path, and then call the setCurrentIndex() and edit() 方法获取 QModelIndex

def NewF(self):
    d = str(self.model.filePath(self.tree.currentIndex())) + '/New folder'
    if not os.path.exists(d):
        os.mkdir(d)
    ix = self.model.index(d)
    QTimer.singleShot(0, lambda ix=ix: self.tree.setCurrentIndex(ix))
    QTimer.singleShot(0, lambda ix=ix: self.tree.edit(ix))

或者使用QFileSystemModelmkdir()方法如下图:

def NewF(self):
    ci = self.tree.currentIndex()
    ix = self.model.mkdir(ci, "New folder")
    QTimer.singleShot(0, lambda ix=ix : self.tree.setCurrentIndex(ix))
    QTimer.singleShot(0, lambda ix=ix : self.tree.edit(ix))