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. QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit

QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.5k Views 1 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.
  • S Offline
    S Offline
    Soul
    wrote on last edited by
    #1

    I wan't to drag a file to the text edit and open it in the background.
    But when I drag the file into text edit, the text edit always append "file:///XXXXXXXXXXXX" to the end of it.
    Is there any way to solve the problem ? Please help.

    A champion is someone who gets up even when he can´t.

    raven-worxR 1 Reply Last reply
    0
    • S Offline
      S Offline
      Soul
      wrote on last edited by
      #2

      The code is shown below:

      
      void ConsoleEdit::dropEvent(QDropEvent *e)
      {
          QFileInfo file(e->mimeData()->text());
      
          if(file.suffix().contains("Bin") ||\
                  file.suffix().contains("bin") ||\
                  file.suffix().contains("BIN")){
              qDebug("support file\r\n");
      
              if(pFile->isOpen()){
                  pFile->close();
              }
              else{
                  pFile->setFileName(e->mimeData()->urls().first().toLocalFile());
                  if(pFile->open(QIODevice::ReadOnly)){
                      emit newBinFileDragInEvent();
                      //appendInfo("New file opened\r\n");
                  }
              }
          }
          else{
              appendInfo("Unsupport file .\r\n", INFO_TYPE_ERR);
          }
      
          QTextEdit::dropEvent(e);
      }
      

      A champion is someone who gets up even when he can´t.

      1 Reply Last reply
      0
      • S Soul

        I wan't to drag a file to the text edit and open it in the background.
        But when I drag the file into text edit, the text edit always append "file:///XXXXXXXXXXXX" to the end of it.
        Is there any way to solve the problem ? Please help.

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

        @Soul
        something like this:

        bool MyTextEdit::canInsertFromMimeData(const QMimeData *source) const
        {
              return source->hasUrls() || QTextEdit::canInsertFromMimeData(source);
        }
        
        void MyTextEdit::insertFromMimeData(const QMimeData *source)
        {
            if (source->hasUrls())
            {
                QFile file (source->urls().first().toLocalFile());
                if ( file.open(QIODevice::ReadOnly) )
                {
                    QTextStream in( &file );
                    this->setPlainText( in.readAll() );
                    return;
                }
           }
           QTextEdit::insertFromMimeData(source);
        }
        

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

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

          @Soul
          something like this:

          bool MyTextEdit::canInsertFromMimeData(const QMimeData *source) const
          {
                return source->hasUrls() || QTextEdit::canInsertFromMimeData(source);
          }
          
          void MyTextEdit::insertFromMimeData(const QMimeData *source)
          {
              if (source->hasUrls())
              {
                  QFile file (source->urls().first().toLocalFile());
                  if ( file.open(QIODevice::ReadOnly) )
                  {
                      QTextStream in( &file );
                      this->setPlainText( in.readAll() );
                      return;
                  }
             }
             QTextEdit::insertFromMimeData(source);
          }
          
          S Offline
          S Offline
          Soul
          wrote on last edited by
          #4

          @raven-worx
          Hi Raven, Thanks for you replying.
          I tryed the code you had given to me. But I think it still can not prevent the dropEvent insert "file:///XXXX" to the end.
          Can I pass something NULL-Event to the QTextEdit::dropEvent() ?

          A champion is someone who gets up even when he can´t.

          raven-worxR 1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Soul said in QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit:

            QTextEdit::dropEvent(e);

            I would guess the default implementation just reads the url from the QMimeData and appends it to the QTextEdit. Try to simply not call the base implementation to avoid this.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • S Soul

              @raven-worx
              Hi Raven, Thanks for you replying.
              I tryed the code you had given to me. But I think it still can not prevent the dropEvent insert "file:///XXXX" to the end.
              Can I pass something NULL-Event to the QTextEdit::dropEvent() ?

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

              @Soul said in QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit:

              But I think it still can not prevent the dropEvent insert "file:///XXXX" to the end.

              do you think or do you know?
              I haven't tested my code, but if there are urls in the mime data they are inserted before any other data (like text).

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

              1 Reply Last reply
              1
              • S Offline
                S Offline
                Soul
                wrote on last edited by
                #7

                @Christian-Ehrlicher Yes it's the reason. But when I do not call the QTextEdit::dropEvent(e), the text cursor is frozened after I drag the file into text area.

                A champion is someone who gets up even when he can´t.

                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