【发布时间】:2018-06-15 17:52:14
【问题描述】:
我正在尝试使用 PyQt5 实现文件目录。我想在此树中合并拖放功能以支持内部和外部文件(即,如果我的桌面上有一些文件,我希望能够将它们拖放到文件夹中在我的 PyQt 视图中)。这是我目前拥有的:
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication,
QMenu, QAbstractItemView
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from src import config
class Tree(QTreeView):
def __init__(self):
QTreeView.__init__(self)
cfg = config.get()
model = QFileSystemModel()
model.setRootPath("/Users/")
self.setModel(model)
self.setRootIndex(model.index("/Users/"))
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.open_menu)
self.setSelectionMode(self.SingleSelection)
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setDropIndicatorShown(True)
def open_menu(self):
menu = QMenu()
menu.addAction("Create new folder")
menu.exec_(QCursor.pos())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
通过上面的代码,我可以显示一个目录及其内容。我可以拖动一个项目,但放下它什么也不做,也看不到放下指示。目前还不清楚如何
一个。在视图中进行拖放操作并
b.让它与该上下文之外的项目一起工作(例如来自桌面)。
【问题讨论】:
标签: python drag-and-drop pyqt pyqt5 qtreeview