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

标签 python python-3.x pyqt5 qtreeview

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

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

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

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

感谢您的建议。

最佳答案

使用您的代码,您必须首先使用 index() 获取 QModelIndex QFileSystemModel 的方法将路径传递给它,然后调用 setCurrentIndex()edit() QTreeView 的方法。

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

或者使用mkdir() QFileSystemModel的方法如下所示:

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

关于python - 如何使用 PYQT5 在 QTreeView 中选择和编辑新创建的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51846892/

相关文章:

python - python调用CNBC后端API

python - Struct.error : argument for 's' must be a bytes object, 已经提供

python - Python 2.7 和 3.6 之间的不同 @patch 行为(使用 mock)

python-3.x - Flask - 请求位于子域的特定端点时出现问题

python - 使用 QSortFilterProxyModel 时出现 pyQt5 段错误

python - 单击 Windows 'X'(关闭)按钮时停止任何循环

python - 删除顶行 2 行并将成绩单放在下面

python - pandas 中的 block 总数

python - 魔法 python 不工作

Python 将 Qt ui 与信号分开