Drag n' drop does not work
-
Hi,
i want to enable drag n' drop for my program, but it does not work. I want to accept files (only .msh but it would be fine if it accept everything, too)to drop on my window and i need the filename and path.
First i set my widget to accept drops
and then i overwrite the drop function
But when i move something over my window, i get an icon that dropping is not allowed.Any ideas?? Do i need the move function, too?? I don't care about where the file is dropped. It can be done everywhere on my window.
-
Hi
Yes, you need the other funcs also
http://doc.qt.io/qt-5/dnd.html
So it knows its ok and will show "ok" symbol.void Window::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain"))
event->acceptProposedAction();
}Also notice the text/plain check.
When dropping files from outside , the format will/might be different.
something likevoid MainWindow::dragEnterEvent(QDragEnterEvent *e) { if (e->mimeData()->hasUrls()) { e->acceptProposedAction(); } } void MainWindow::dropEvent(QDropEvent *e) { foreach (const QUrl &url, e->mimeData()->urls()) { QString fileName = url.toLocalFile(); qDebug() << "Dropped file:" << fileName; } }
- It can be done everywhere on my window.
No, it only works for the widget that implements the D&D.
So if u drag over another widget it wont work.
- It can be done everywhere on my window.
-
works, thanks :D