/ Published in: C++
You have to implement 2 protected virtual methods in your widget you want to accept file or directory drops: - void dragEnterEvent(QDragEnterEvent *event) - void dropEvent(QDropEvent *event)
And also you have to call setAcceptDrops(true); on this widget before it could accept drops.
Expand |
Embed | Plain Text
void MainWindow::dragEnterEvent(QDragEnterEvent *event) { if(event->mimeData()->hasUrls()) { event->acceptProposedAction(); } } void MainWindow::dropEvent(QDropEvent *event) { QList<QUrl> droppedUrls = event->mimeData()->urls(); int droppedUrlCnt = droppedUrls.size(); for(int i = 0; i < droppedUrlCnt; i++) { QString localPath = droppedUrls[i].toLocalFile(); QFileInfo fileInfo(localPath); if(fileInfo.isFile()) { // file QMessageBox::information(this, tr("Dropped file"), fileInfo.absoluteFilePath()); } else if(fileInfo.isDir()) { // directory QMessageBox::information(this, tr("Dropped directory"), fileInfo.absoluteFilePath()); } else { // none QMessageBox::information(this, tr("Dropped, but unknown"), tr("Unknown: %1").arg(fileInfo.absoluteFilePath())); } } event->acceptProposedAction(); }
You need to login to post a comment.
