Hi,

I would like to use QTextEdit with drag-n-drop but with CUT-n-PASTE and not the default COPY-n-PASTE.

To do this I I subclassed QTextEdit and and re-implemented
Qt Code:
  1. dragEnterEvent
To copy to clipboard, switch view to plain text mode 
and
Qt Code:
  1. dropEvent(QDropEvent *event)
To copy to clipboard, switch view to plain text mode 
. However,. even though it now works with cut-n-paste the cursor is all screwed up.

I.e. the cursor position is where I SELECTED the text and not where I dropped the text. IF I change that (setPosition, setTextCursor etc) I still end up with the problem that if I click somewhere on the document the 'ACTUAL' cursor moves to where I click BUT it is invisible, the cursor symbol is where the cursor was BEFORE I clicked on the document....

I COULD probably fix it by re-implementing more mouse events (mousePress, etc) but I think it should be possible to solve much easier.

So my questions are
1) Best way of getting cut-n-paste instead of copy-n-paste for drag-n-drop of text in a QTextEdit?

2) If it IS by subclassing dragEnterEvent and dropEvent then how can I fix the cursor problem?


See my code below for how it is implemented now
Qt Code:
  1. void TextEdit::dragEnterEvent(QDragEnterEvent *e)
  2. {
  3. e->acceptProposedAction();
  4. }
  5.  
  6.  
  7. void TextEdit::dropEvent(QDropEvent *event)
  8. {
  9. event->acceptProposedAction();
  10.  
  11. if ( event->dropAction() == Qt::CopyAction)
  12. {
  13.  
  14.  
  15. QTextCursor cursor = textCursor(); // copy of cursor
  16.  
  17. // Drop position cursor
  18. QTextCursor insertionCursor = cursorForPosition( event->pos() );
  19.  
  20. insertionCursor.beginEditBlock(); /* Start atomic operation (undo/redo) */
  21.  
  22. QTextDocumentFragment text = cursor.selection(); /* Extract text before removing selected area */
  23.  
  24. cursor.removeSelectedText(); /* Remove from original position */
  25. insertionCursor.insertFragment( text ); /* Insert dragged text/image/link */
  26.  
  27. // Tried with set setTextCursor here but that only moved the visible cursor
  28. // initially. Clicking on the document still gives an active invisible cursor
  29. // AND a non-active visible cursor symbol
  30.  
  31. insertionCursor.endEditBlock(); /* End atomic operation (undo/redo) */
  32. }
  33. }
To copy to clipboard, switch view to plain text mode