Results 1 to 3 of 3

Thread: PyQt – Load data from .txt file via Drag and Drop

  1. #1
    Join Date
    Mar 2017
    Posts
    4
    Qt products
    Platforms
    Windows

    Default PyQt – Load data from .txt file via Drag and Drop

    Hello everyone!

    I have a folder TreeView on the left layout and I want to drag a .txt file from there and drop it to the other layout. Hopefully, I want to load the data of that dropped file on a variable.
    For the code’s needs, I used till now (to my “real” code) the np.loadtxt() to load the data, so I’d like to use it here too.
    In case it matters, the .txt file contains 4 columns (coordinates).

    I post my code. The program closes when I drop the file.

    Thanks in advance!

    Qt Code:
    1. import sys, time, os
    2.  
    3. from PyQt5.QtWidgets import *
    4. from PyQt5.QtGui import *
    5. from PyQt5.QtCore import *
    6.  
    7. import numpy as np
    8. import pylab as pl
    9.  
    10. import random
    11.  
    12. class Example(QMainWindow):
    13.  
    14. def __init__(self):
    15. super().__init__()
    16.  
    17. self.initUI()
    18.  
    19.  
    20. def initUI(self):
    21.  
    22. self.central_widget = QWidget()
    23. self.setCentralWidget(self.central_widget)
    24.  
    25.  
    26. self.folderLayout = QWidget();
    27.  
    28. self.pathRoot = QDir.rootPath()
    29.  
    30. self.dirmodel = QFileSystemModel(self)
    31. self.dirmodel.setRootPath(QDir.currentPath())
    32.  
    33. self.indexRoot = self.dirmodel.index(self.dirmodel.rootPath())
    34.  
    35. self.folder_view = QTreeView();
    36. self.folder_view.setDragEnabled(True)
    37. self.folder_view.setModel(self.dirmodel)
    38. self.folder_view.setRootIndex(self.indexRoot)
    39.  
    40. self.selectionModel = self.folder_view.selectionModel()
    41.  
    42. self.left_layout = QVBoxLayout()
    43. self.left_layout.addWidget(self.folder_view)
    44.  
    45. self.folderLayout.setLayout(self.left_layout)
    46.  
    47. splitter_filebrowser = QSplitter(Qt.Horizontal)
    48. splitter_filebrowser.addWidget(self.folderLayout)
    49. splitter_filebrowser.addWidget(Figure_Canvas(self))
    50. splitter_filebrowser.setStretchFactor(1, 1)
    51.  
    52. hbox = QHBoxLayout(self)
    53. hbox.addWidget(splitter_filebrowser)
    54.  
    55. self.centralWidget().setLayout(hbox)
    56.  
    57.  
    58. self.setWindowTitle('Simple drag & drop')
    59. self.setGeometry(750, 100, 600, 500)
    60.  
    61.  
    62.  
    63. class Figure_Canvas(QWidget):
    64.  
    65. def __init__(self, parent):
    66. super().__init__(parent)
    67.  
    68. self.setAcceptDrops(True)
    69.  
    70. blabla = QLineEdit()
    71.  
    72. self.right_layout = QVBoxLayout()
    73. self.right_layout.addWidget(blabla)
    74.  
    75.  
    76. self.buttonLayout = QWidget()
    77. self.buttonLayout.setLayout(self.right_layout)
    78.  
    79. def dragEnterEvent(self, e):
    80.  
    81. if e.mimeData().hasFormat('text/uri-list'):
    82. e.accept()
    83. else:
    84. e.ignore()
    85.  
    86. def dropEvent(self, e):
    87.  
    88. print("something")
    89. data = np.loadtxt(e.mimeData())
    90. print(data)
    91.  
    92.  
    93. if __name__ == '__main__':
    94.  
    95. app = QApplication(sys.argv)
    96. ex = Example()
    97. ex.show()
    98. app.exec_()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt – Load data from .txt file via Drag and Drop

    Can "np.loadtxt" handle a QMimeData object as its argument?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2017
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: PyQt – Load data from .txt file via Drag and Drop

    Hi anda_skoa, thanks for your quick reply!
    Truth is I have no idea, but I’m checking it out now in case I find something and I’ll post.
    Have you any clue if it could be done in another way? For example to get the file name (e.g. this_file.txt) and then use something like:

    Qt Code:
    1. with open("test_5.txt") as f:
    2. data2 = np.loadtxt("this_file.txt ")
    To copy to clipboard, switch view to plain text mode 

    Thanks again!

Similar Threads

  1. [PyQT] Drag and Drop of subclassed QListWidgetItem
    By Gad82 in forum Qt Programming
    Replies: 0
    Last Post: 24th June 2014, 09:41
  2. Drag and Drop file into Qt Quick window
    By marwooj in forum Qt Quick
    Replies: 0
    Last Post: 23rd June 2011, 04:04
  3. Drag and Drop QTableWidget in UI file.
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 23:02
  4. Replies: 1
    Last Post: 8th January 2009, 17:40
  5. Drag & Drop: delayed file delivery (Win & Mac)
    By Conel in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2007, 16:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.