Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PyQt–;通过拖放从.txt文件加载数据_Qt_Drag And Drop_Pyqt_Pyqt5_Qtreeview - Fatal编程技术网

PyQt–;通过拖放从.txt文件加载数据

PyQt–;通过拖放从.txt文件加载数据,qt,drag-and-drop,pyqt,pyqt5,qtreeview,Qt,Drag And Drop,Pyqt,Pyqt5,Qtreeview,我在左边的布局上有一个文件夹TreeView,我想从那里拖动一个.txt文件并将其放到另一个布局上。希望我能将删除文件的数据加载到一个变量上 出于代码的需要,到目前为止(对于我的“真实”代码),我一直使用np.loadtxt()来加载数据,所以我也希望在这里使用它。 如果有必要,则.txt文件包含4列(坐标) 我发布我的代码。当我删除文件时,程序关闭 提前谢谢 import sys, time, os from PyQt5.QtWidgets import * from PyQt5.QtGui

我在左边的布局上有一个文件夹TreeView,我想从那里拖动一个.txt文件并将其放到另一个布局上。希望我能将删除文件的数据加载到一个变量上

出于代码的需要,到目前为止(对于我的“真实”代码),我一直使用np.loadtxt()来加载数据,所以我也希望在这里使用它。 如果有必要,则.txt文件包含4列(坐标)

我发布我的代码。当我删除文件时,程序关闭

提前谢谢

import sys, time, os

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

import numpy as np
import pylab as pl

import random

class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)


        self.folderLayout = QWidget();

        self.pathRoot = QDir.rootPath()

        self.dirmodel = QFileSystemModel(self)
        self.dirmodel.setRootPath(QDir.currentPath())

        self.indexRoot = self.dirmodel.index(self.dirmodel.rootPath())

        self.folder_view = QTreeView();
        self.folder_view.setDragEnabled(True)
        self.folder_view.setModel(self.dirmodel)
        self.folder_view.setRootIndex(self.indexRoot)

        self.selectionModel = self.folder_view.selectionModel()

        self.left_layout = QVBoxLayout()
        self.left_layout.addWidget(self.folder_view)

        self.folderLayout.setLayout(self.left_layout)        

        splitter_filebrowser = QSplitter(Qt.Horizontal)
        splitter_filebrowser.addWidget(self.folderLayout)
        splitter_filebrowser.addWidget(Figure_Canvas(self))
        splitter_filebrowser.setStretchFactor(1, 1)

        hbox = QHBoxLayout(self)
        hbox.addWidget(splitter_filebrowser)

        self.centralWidget().setLayout(hbox)


        self.setWindowTitle('Simple drag & drop')
        self.setGeometry(750, 100, 600, 500)



class Figure_Canvas(QWidget):

    def __init__(self, parent):
        super().__init__(parent)

        self.setAcceptDrops(True)

        blabla = QLineEdit()

        self.right_layout = QVBoxLayout()
        self.right_layout.addWidget(blabla)


        self.buttonLayout = QWidget()
        self.buttonLayout.setLayout(self.right_layout)

    def dragEnterEvent(self, e):

        if e.mimeData().hasFormat('text/uri-list'):
            e.accept()
        else:
            e.ignore() 

    def dropEvent(self, e):

        print("something")
        data = np.loadtxt(e.mimeData())
        print(data)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()             

不幸的是,PyQt5使插槽中的错误消息静音。请遵循并报告具体的堆栈跟踪和错误消息。我的意思是回答中的解决方案: