Solved dragEnterEvent and dropEvent
-
Now ,I want to realise a NoteBook which has QTextEdit object . I want to drag existing .txt file into app and textedit can display the content of the .txt file. I override dragEnterEvent and dropEvent.
//your code here void MainWindow::dragEnterEvent(QDragEnterEvent *event) { const QMimeData *mimedata= event->mimeData(); if(mimedata->hasUrls()) { // event->setAccepted(true); event->acceptProposedAction(); qDebug()<<tr("dragEnterEvent"); } else { event->setAccepted(false); event->ignore(); } } void MainWindow::dropEvent(QDropEvent *event) { const QMimeData *mimedata= event->mimeData(); qDebug()<<tr("dropEvent"); if(mimedata->hasUrls()) { QList<QUrl> urllist = mimedata->urls(); QString filename = urllist.at(0).toLocalFile(); if(!filename.isEmpty()) { qDebug()<<filename; loadfile(filename); } } }
But there is a problem.when i drag txt file to the position near menubar ,textdit can diplay right like tip1.But when i dag it into text edit,it will diplay content and url like tip2.
tip1: it is just a test
tip2:it is just a testfile:///C:/Users/chaochao/Desktop/1.txtThank for your help
-
Hi
you seems only to override for mainwindow.
If you want to drop to textedit it should have same code to do the same. -
-
@mrjj thanks, I soloved it by your help
-
@raven-worx thanks, textedit also need override virtual function