当我将文件拖放到QstandardModel中的项目之一时,我有一个QstandardItemModel和一个File模型,我希望将其拖放到该行上。 这是我的后续代码
import sys
import paramiko
from PyQt5.QtWidgets import QApplication,QFileSystemModel,
QProgressBar,QTreeView, QWidget, QVBoxLayout,QHBoxLayout,
QAbstractItemView
from PyQt5.QtCore import QDir, QFile, QFileInfo
from PyQt5.QtGui import *
data ={
"xyz": [],
"abc":[]
}
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setAcceptDrops(True)
self.mytreeview = QTreeView(self)
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.mytreeview)
self.model = QStandardItemModel(self.mytreeview)
self.mytreeview.setModel(self.model)
self.setAcceptDrops(True)
self.initialise_model()
def initialise_model(self):
for text in data:
item = QStandardItem(text)
icon=QIcon('...')
item.setIcon(icon)
item.setDragEnabled(True)
self.model.appendRow(item)
print("item index")
print(item.row())
for c in data[text]:
type=c.split(".")[1]
children = QStandardItem(c)
if type == 'pdf':
pdficon=QIcon('...')
children.setIcon(pdficon)
elif type == 'doc':
docicon=QIcon('...')
children.setIcon(docicon)
item.appendRow(children)
def dragEnterEvent(self, event):
print("the eveney")
m = event.mimeData()
if m.hasUrls():
for url in m.urls():
if url.isLocalFile():
path = url.toLocalFile()
info = QFileInfo(path)
filename=info.fileName()
paths=info.absoluteFilePath()
print("the file path")
print(paths)
event.accept()
def dropEvent(self, event):
print("Drop EVent triuggered")
# item = self.model().itemFromIndex(index)
# print(index)
pos=event.pos()
print(pos)
print(self.model.item(0,0).row())
m = event.mimeData()
if m.hasUrls():
for url in m.urls():
print("indie")
print(url.toString())
if url.isLocalFile():
event.accept()
class App(QWidget):
class Tree(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath(QDir.currentPath())
self.setModel(model)
self.setRootIndex(model.index("/home/mohit/"))
model.setReadOnly(False)
self.setSelectionMode(self.MultiSelection)
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setDropIndicatorShown(True)
window_layout = QVBoxLayout()
# window_layout.addWidget(self)
# window_layout.addWidget(self)
self.setLayout(window_layout)
def dragEnterEvent(self, event):
print(self)
m = event.mimeData()
if m.hasUrls():
for url in m.urls():
if url.isLocalFile():
event.accept()
return
event.ignore()
def dropEvent(self, event):
print('DROP ::: '.format(self))
if event.source():
QTreeView.dropEvent(self, event)
else:
ix = self.indexAt(event.pos())
if not self.model().isDir(ix):
ix = ix.parent()
pathDir = self.model().filePath(ix)
m = event.mimeData()
if m.hasUrls():
urlLocals = [url for url in m.urls() if
url.isLocalFile()]
accepted = False
for urlLocal in urlLocals:
path = urlLocal.toLocalFile()
info = QFileInfo(path)
n_path = QDir(pathDir).filePath(info.fileName())
o_path = info.absoluteFilePath()
if n_path == o_path:
continue
if info.isDir():
QDir().rename(o_path, n_path)
else:
qfile = QFile(o_path)
if QFile(n_path).exists():
n_path += "(copy)"
qfile.rename(n_path)
accepted = True
if accepted:
event.acceptProposedAction()
def __init__(self):
super().__init__()
self.title = 'POC For File upload.'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.tree_obj = self.Tree()
self.tree_obj2 = Widget()
pbar = QProgressBar(self)
pbar.setGeometry(30, 40, 200, 25)
windowLayout = QHBoxLayout()
windowLayout.addWidget(self.tree_obj2)
windowLayout.addWidget(self.tree_obj)
vbox = QVBoxLayout()
vbox.addLayout(windowLayout)
vbox.addWidget(pbar)
self.setLayout(vbox)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
我需要行号,因为我将找到项目详细信息,这将有助于我将文件上传到特定文件夹中