Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. dragEnterEvent and dropEvent
Forum Updated to NodeBB v4.3 + New Features

dragEnterEvent and dropEvent

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • chaochaoC Offline
    chaochaoC Offline
    chaochao
    wrote on last edited by
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      chaochaoC 1 Reply Last reply
      1
      • chaochaoC chaochao

        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

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        chaochaoC 1 Reply Last reply
        2
        • mrjjM mrjj

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

          chaochaoC Offline
          chaochaoC Offline
          chaochao
          wrote on last edited by
          #4

          @mrjj thanks, I soloved it by your help

          1 Reply Last reply
          1
          • raven-worxR raven-worx

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

            chaochaoC Offline
            chaochaoC Offline
            chaochao
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved