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
dragEnterEvent
dragEnterEvent
To copy to clipboard, switch view to plain text mode
and
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
{
e->acceptProposedAction();
}
{
event->acceptProposedAction();
if ( event->dropAction() == Qt::CopyAction)
{
// Drop position cursor
QTextCursor insertionCursor
= cursorForPosition
( event
->pos
() );
insertionCursor.beginEditBlock(); /* Start atomic operation (undo/redo) */
cursor.removeSelectedText(); /* Remove from original position */
insertionCursor.insertFragment( text ); /* Insert dragged text/image/link */
// Tried with set setTextCursor here but that only moved the visible cursor
// initially. Clicking on the document still gives an active invisible cursor
// AND a non-active visible cursor symbol
insertionCursor.endEditBlock(); /* End atomic operation (undo/redo) */
}
}
void TextEdit::dragEnterEvent(QDragEnterEvent *e)
{
e->acceptProposedAction();
}
void TextEdit::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
if ( event->dropAction() == Qt::CopyAction)
{
QTextCursor cursor = textCursor(); // copy of cursor
// Drop position cursor
QTextCursor insertionCursor = cursorForPosition( event->pos() );
insertionCursor.beginEditBlock(); /* Start atomic operation (undo/redo) */
QTextDocumentFragment text = cursor.selection(); /* Extract text before removing selected area */
cursor.removeSelectedText(); /* Remove from original position */
insertionCursor.insertFragment( text ); /* Insert dragged text/image/link */
// Tried with set setTextCursor here but that only moved the visible cursor
// initially. Clicking on the document still gives an active invisible cursor
// AND a non-active visible cursor symbol
insertionCursor.endEditBlock(); /* End atomic operation (undo/redo) */
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks