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.txt

    Thank for your help


  • Qt Champions 2016

    Hi
    you seems only to override for mainwindow.
    If you want to drop to textedit it should have same code to do the same.


  • Moderators

    @chaochao
    to add up to @mrjj
    Depending on how complex your widget structure is, it may also be enough to setAcceptDrops(false) on the text edit. So the drag events are propagated up the parent chain until your main window receives it.



  • @mrjj thanks, I soloved it by your help



  • @raven-worx thanks, textedit also need override virtual function


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.