Question

I have a QListWidget which I fill with filenames (the filename without path is the item's text, and the full path is in the item's tooltip). I want to be able to drag, for example, a movie file to VLC from my QListWidget, and VLC should start to play it - the same as if I had dragged it from a file manager.

I've tried reading the documentation on Drag&Drop, but couldn't figure out how to do it. I have set dragEnabled property to true, and dragDropMode property to DragOnly. Now I can start a drag, but if I drag a list item to VLC nothing happens (which isn't surprising).

Was it helpful?

Solution

I wanted to do the exact same thing, here is what I came up with. It works on Windows, but I haven't tested on other platforms.

class CustomListWidget : public QListWidget
{
    public :
        CustomListWidget( QWidget * parent = 0 ) : QListWidget( parent ) {}
    protected :
        QStringList mimeTypes() const
        {
            QStringList qstrList;
            qstrList.append("text/uri-list");
            return qstrList;
        }
        QMimeData * mimeData( const QList<QListWidgetItem *> items ) const
        {
            QMimeData *data = new QMimeData();
            QList< QUrl > urls;
            QUrl url;
            url.setPath( items[ 0 ]->toolTip() );
            urls.append( url );
            data->setUrls( urls );
            return data;
        }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top