PDA

View Full Version : QListView not accepting external drops



battersea
12th November 2010, 16:20
Hi,

I have an application consisting of a simple QLabel:


class MyLabel : public QLabel {
Q_OBJECT
public:
MyLabel(QWidget * parent = 0, Qt::WindowFlags f = 0) : QLabel(parent,f) {
setAcceptDrops(true);
setDropIndicatorShown(true);
};

virtual ~MyLabel() {};

private:
void dragEnterEvent(QDragEnterEvent* event) {
std::cout << "MyLabel::dragEnterEvent()::begin" << std::endl;
event->acceptProposedAction();
std::cout << "MyLabel::dragEnterEvent()::end" << std::endl;
}

void dropEvent(QDropEvent* event) {
std::cout << "MyLabel::dropEvent()::begin" << std::endl;
std::cout << "MyLabel::dropEvent()::end" << std::endl;
}
};

and a QListView:


class MyListView : public QListView {
Q_OBJECT
public:
MyListView(QWidget * parent = 0) : QListView(parent) {
setAcceptDrops(true);
setDropIndicatorShown(true);
};

virtual ~MyListView() {};

private:
void dragEnterEvent(QDragEnterEvent* event) {
std::cout << "MyListView::dragEnterEvent::begin" << std::endl;
event->acceptProposedAction();
std::cout << "MyListView::dragEnterEvent::end" << std::endl;
}

void dropEvent(QDropEvent* event) {
std::cout << "MyListView::dropEvent::begin" << std::endl;
std::cout << "MyListView::dropEvent::end" << std::endl;
}
};

The QLabel widget accepts drops from files outside the Qt application, resulting in the output:

MyLabel::dragEnterEvent::begin
MyLabel::dragEnterEvent::end
MyLabel::dropEvent::begin
MyLabel::dropEvent::end

The QListview widget does not accept drops from files outside the Qt application, resulting in the output:

MyListView::dragEnterEvent::begin
MyListView::dragEnterEvent::end

So even though the proposed action is accepted in MyListView::dragEnterEvent, the MyListView::dropEvent function is never called...

So my question is the following:

What's keeping the QListView from calling the dropEvent?

Eventually, I will want to add the file names of the dropped files into the list, but without the dropEvent ever being called, I don't see how I can do this...

Cheers,
Ben

battersea
15th November 2010, 19:46
void dragMoveEvent(QDragMoveEvent* event) {
event->acceptProposedAction();
}

seems to fix the issue.

The parent "dragMoveEvent" was ignoring the event. See also this thread: http://www.qtcentre.org/threads/34985-Drag-Drop-Drop-indicator-doesn-t-change-for-drags-between-QListView-and-QTableView