简体   繁体   English

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

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

I am trying to figure out how to select and edit newly created folder. 我试图弄清楚如何选择和编辑新创建的文件夹。 Here is a bit of code demonstrating it: 这是一些代码演示它:

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_())

After creating the new folder, I would like it to be selected and in edit mode at same time (ie: self.tree.edit(correctIndex)). 创建新文件夹后,我希望同时选择它并使其处于编辑模式(即:self.tree.edit(correctIndex))。

I have checked some posts ( here ) but still have not managed to get the correct index. 我已经检查了一些帖子( 在这里 ),但仍然没有设法获得正确的索引。

Thanks for suggestions. 感谢您的建议。

Using your code you must first obtain the QModelIndex using the index() method of QFileSystemModel passing it the path, and then call the setCurrentIndex() and edit() methods of QTreeView. 使用您的代码,必须首先使用QModelIndexindex()方法传递路径来获取QModelIndex ,然后调用QFileSystemModelsetCurrentIndex()edit()方法。

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))

Or use the mkdir() method of QFileSystemModel as shown below: 或使用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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM