PySide: Drag and drop files into QListWidget

Related searches

This is my first entry here so apologies for any newbie mistakes. I've searched both here and Google with no luck before posting.

I want to be able to add images to a QListWidget, using drag and drop from a file browser. Dropping a valid file on the list widget also needs to trigger a function in the main class of my app, and pass it the image path.

I found this code that does exactly that, but for PyQt4. Importing QtCore and QtGui from PySide instead of PyQt4 produces a segmentation fault when triggering a drag and drop event. There are no error messages.

I think I've traced this to an old way of handling signals and have tried using the new, more pythonic way described here. I can't seem to get it working, while still passing a list of URLs between classes however.

Any help would be greatly appreciated!

import sys
import os
from PyQt4 import QtGui, QtCore

class TestListView(QtGui.QListWidget):
    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.QSize(72, 72))

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            event.ignore()

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestListView(self)
        self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
        self.setCentralWidget(self.view)

    def pictureDropped(self, l):
        for url in l:
            if os.path.exists(url):
                print(url)                
                icon = QtGui.QIcon(url)
                pixmap = icon.pixmap(72, 72)                
                icon = QtGui.QIcon(pixmap)
                item = QtGui.QListWidgetItem(url, self.view)
                item.setIcon(icon)        
                item.setStatusTip(url)        

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

Found out how PySide signals work in the end. Here's the PyQt4 code above, ported to PySide. I'd love to hear if there are better solutions out there.

import sys
import os
from PySide import QtGui, QtCore

class TestListView(QtGui.QListWidget):

    fileDropped = QtCore.Signal(list)

    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.QSize(72, 72))

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.fileDropped.emit(links)
        else:
            event.ignore()

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestListView(self)
        self.view.fileDropped.connect(self.pictureDropped)
        self.setCentralWidget(self.view)

    def pictureDropped(self, l):
        for url in l:
            if os.path.exists(url):
                print(url)                
                icon = QtGui.QIcon(url)
                pixmap = icon.pixmap(72, 72)                
                icon = QtGui.QIcon(pixmap)
                item = QtGui.QListWidgetItem(url, self.view)
                item.setIcon(icon)        
                item.setStatusTip(url)        

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

Pyside example of dragging and dropping a file onto a GUI. See http , See http://www.birving.com/blog/2015/09/29/drag-and-drop-files-into-gui-using/. pyside_drag_drop_image_file.py. """ A simple example pyside app that demonstrates dragging and dropping. of files onto a GUI QWidget):. """. PySide: Drag and drop files into QListWidget. 1. PyQt - object has no attribute 'connect' 5. Drag and drop with pyqt5 (SIGNAL) 1. PyQt4 - Dragging and dropping files

For me this worked fine (tested on PySide2 & PyQt5). This is not a drop signal, but a signal when the rows of a QListWidget have been moved. In my case this is always happening after dropping.

self.window.my_list_widget.model().rowsMoved.connect(self.my_method)

Drag and Drop in PySide, In this part of the PySide tutorial, we will talk about drag & drop operations. In computer QWidget): def __init__(self): super(Example, self). PySide: Drag and drop files into QListWidget. 11. Drag and drop rows within QTableWidget. 0. PyQt: QListView drag and drop reordering signal issue. 0.

I made some changes of @thimic 's code, so that it can run in my computer:

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

class TestListView(QListWidget):


    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.QSize(72, 72))

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            print('Has drop Event! ')
        else:
            event.ignore()

class MainForm(QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestListView(self)
        self.view.fileDropped.connect(self.pictureDropped)
        self.setCentralWidget(self.view)

    def pictureDropped(self, l):
        for url in l:
            if os.path.exists(url):
                print(url)
                icon = QtGui.QIcon(url)
                pixmap = icon.pixmap(72, 72)
                icon = QtGui.QIcon(pixmap)
                item = QtGui.QListWidgetItem(url, self.view)
                item.setIcon(icon)
                item.setStatusTip(url)

def main():
    app = QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

Drag and Drop items in QListWidget, Hello, i've got an list widget that should be able to accept file drops and be able of drag and drop the items into the desired order. if i do all this� Hello My Friends I have a GUI qt application with a QListWidget. i need to add file and directory path to QListWidget after drag and drop inside it. I'm newbie in QT. I'll be appreciated if anyone give me a simple way to do this. thank you very much.

Drag and drop with Qt and PySide — Riham Toulan, Starting with a convenient behaviour : Drag and Drop. methods of a QLabel and a QWidget so we are able to drag the label into the widget. Drag and drop files into a GUI using python and pyside. QtGui import * import sys import types class MainFrame(QWidget. Active 2 months ago. You can rearrange, float, close, and restore Qt panels at runtime, giving every application a highly configurable usability-centric interface.

implement drag and drop files and directory to qlistwidget, Hello My Friends I have a GUI qt application with a QListWidget. i need to add file and directory path to QListWidget after drag and drop inside it� How to have drag and drop file feature for QlistWidget along with internal move option in PyQt 0 pyqt : Display All values present in QListWidget as selected to user. @param itemName Item's display name. selectedItems() Complete code. QListWidget uses an internal model to manage each QListWidgetItem in the list.

QListWidget is a convenience class that provides a list view similar to the one supplied by QListView, but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. For a more flexible list view widget, use the QListView class with a standard model.

Comments
  • Hi there @thimic I know this post is old, but this worked well for me but I dont quite understand how this works, do you know of a good explanation video or documentation that I could go through to understand it better ?
  • Hi @JodyStocks, There’s an example of PySide and signals and slots here: zetcode.com/gui/pysidetutorial/eventsandsignals Or if there are particular things you’re wondering about, I’ll try my best to help answer them.
  • Hi there @thimic So I have been trying to get a better understanding of this but I am struggling a little. I have the Drop event Firing correctly but when It Fires, the actually move of the items in my QListWidget are not moving. If I set my Table Variable to be just a normal QListWidget() then the items move and update position. Is there no way to just trigger an event like you do with button clicks ?
  • Hi @JodyStocks, I am not quite sure what you mean about the items in the QListWidget not moving. Are you wishing to be able to drag files onto the widget from outside and also re-arrange the items in the widget internally?
  • @JodyStocks, As for the other question, there are two concepts at play. Events is one and Signals and Slots is another. Interactions in Qt cause events to fire that can be handled by implementing certain methods, such as dropEvent(). Separately, it is possible to set up communication between widgets using signals and slots, such as "fileDropped" and "clicked". This answer explains the difference in a bit more detail: stackoverflow.com/a/3794944/3996477
  • It would be helpful to the community if you explained a bit about what you needed to change and why.