Article directory
Implement drag-and-drop functionality for files
Just imagine, we want to open a python project folder or script in the IDE. Whether we choose the folder path in the IDE or choose how to open the script, we need to spend some effort to click and select it. Or, we want to run a python script on the command line and need to pass in a file path parameter. It is quite troublesome to type the path by hand. If the above situations could be replaced by dragging and dropping files, it would undoubtedly be convenient and easy.
PS: The method introduced below is only successfully tested under Windows.
1 Drag and drop files into the QT window
In order to add the function of dragging and dropping files into the form and performing operations on the QT window, here is an example. We wrote a simple 3D model file visualization software. Even though there is an "open file" option in the software, it is still very troublesome in some cases. We hope to be able to drag the model file directly into the window of the software for visual drawing.
1.1 Implementation method
Refer to this article: Qt main window implements dragging files and displaying them in the text box
Add a line in the window initialization function
self.setAcceptDrops(True) # 设置接受拖拽
Then override dragEnterEvent
and dropEvent
two methods
def dragEnterEvent(self, a0: QtGui.QDragEnterEvent) -> None: # 拖动进入事件
if a0.mimeData().hasUrls():
a0.acceptProposedAction()
else:
a0.ignore()
def dropEvent(self, a0: QtGui.QDropEvent) -> None: # 放下事件
mimeData = a0.mimeData()
if mimeData.hasUrls():
urlList = mimeData.urls()
filename = urlList[0].toLocalFile() # 得到了文件的路径
if filename: # 下面便是对文件的相关操作
# 绘制图形
_, suffix = os.path.splitext(filename)
self.SceneManager.ClearAll()
if suffix == '*.xyz': # 点云绘制
self.original_model = read_xyz(filename)
self.SceneManager.drawPdSrc(self.original_model, (241 / 255, 135 / 255, 184 / 255), point_size = 3)
else:
self.original_model = ReadPolyData(filename).GetOutput()
self.SceneManager.drawPdSrc(self.original_model)
self.SceneManager.display()
1.2 Effect demonstration
2 Drag and drop the file to the python script
It is known that in Windows systems, python script files are .py files, and this file does not support dragging and dropping other files onto it. We want to implement dragging and dropping other files onto the script file as its input parameters and running the script.
2.1 Implementation method
Create a new pyfile_droppable.reg
file, enter the following content and save:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"
[HKEY_CLASSES_ROOT\Python.File\Shell\open\command]
@=""<python interpreter path>" "%L" %*"
Double-click to run this file to complete the addition of registry-related information. Related principles reference: Make Python Scripts Droppable in Windows .
The above reg file adds two items to the registry. The function of lines 3 and 4 is to enable the python script file to receive files dragged and dropped on it (get the path of the file). The function of lines 6 and 7 is Specify the path to the interpreter for running python scripts. By opening the registry, we found that we have mainly modified the two places in the picture below.
Next, we create a new test script droppable_test.py
with the following content:
import os
import sys
py_dir = os.path.dirname(__file__) # 当前脚本所在的目录
result_path = os.path.join(py_dir, 'result.txt') # 生成的结果文件放在脚本目录下
args = sys.argv
# 合并内容并生成新文件
with open(args[1], 'rb') as f:
data1 = f.read()
with open(args[2], 'rb') as f:
data2 = f.read()
with open(result_path, 'wb') as f:
f.write(data1 + data2)
print("文件合并成功!结果文件保存至:" + result_path)
input("按任意键关闭窗口...")
2.2 Effect demonstration
Note that the order in which multiple files are passed into the py script as parameters is the order in which multiple files are selected, that is, from top to bottom and from left to right.