3

I am trying to write a simple program (nothing special) which has a QListView and some Buttons.

My question is: How do I specifically tell the QListView to accept Drag and Drop from the filesystem?

I currently have

setAcceptDrops(true)

Which is OK, but the drag and drop works on the whole (Main)Window. I just want it to work when the file is dragged into the QListView.

Why doesn't this work?:

ui->listView->setAcceptDrops(true);

The whole code:

#include "player.h"
#include "ui_player.h"
#include <QListView>

Player::Player(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Player)
{
    ui->setupUi(this);
    setAcceptDrops(true);
    //This doesnt work:
    //ui->listView->setAcceptDrops(true);
}

Player::~Player()
{
    delete ui;
}

void Player::dropEvent(QDropEvent *ev)
{
    QList<QUrl> urls = ev -> mimeData() -> urls();
    foreach(QUrl url, urls)
    {
        qDebug() << url.toString();
    }
    ev->acceptProposedAction();
}

void Player::dragEnterEvent(QDragEnterEvent *ev)
{
    ev->acceptProposedAction();
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
dizzy
  • 33
  • 3
  • Have my answer solved your problem? If yes, please mark it a as accepted. – LogicStuff Nov 21 '15 at 18:35
  • I have the "Player"-class, which inherits QMainWindow and sets up the ui and I have the class you suggested "MyListView", which inherits QListView. But I can't figure out how to put everything together. Can you give me a basic example? – dizzy Nov 22 '15 at 17:40
  • When I was using custom widgets, I had to build my ui with commands in the constructor of the main window because I could not get widget promotion working because of reasons... I have never done it, but here is a [reference](http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html) and some [SO question](http://stackoverflow.com/questions/4907766/promoting-widgets-in-qt-creator). Don't know if I should be saying it here, but there are even YouTube videos. Keywords: qt promote widget – LogicStuff Nov 22 '15 at 17:52
  • Was the widget promotion successful then? – LogicStuff Nov 22 '15 at 21:18
  • I solved the Problem with your suggested class. I created a pointer to the Object: `myList = new MyListView(ui->listView);` In the Constructor of MyListView I give the Widget as Parameter and set `setAcceptDrops(true);` . Thank you very much – dizzy Nov 23 '15 at 07:25

1 Answers1

2

You should override these event functions for QListView, not QMainWindow. When you do ui->listView->setAcceptDrops(true);, QListView is now the widget that reacts to the drop events, by calling its virtual dropEvent and dragEnterEvent functions.

Make your own class that inherits QListView and define dropEvent and dragEnterEvent in there:

class MyListView
{
public:
    MyListView(QWidget *parent);                        // implement

protected:
    void dropEvent(QDropEvent *ev) override;            // implement
    void dragEnterEvent(QDragEnterEvent *ev) override;  // implement
};

You might also want to override dragMoveEvent as the reference says.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74