PDA

View Full Version : Cursor stops being redrawn when QTextEdit::dropEvent() overrided



Workeml
10th November 2008, 20:43
When I override QTextEdit::dropEvent() and do a drag and drop of text, the cursor stops blinking, stays at its last position, and is no longer redrawn. Is there a call that can be made to restore the cursor drawing?

I've tried calling ensureCursorVisible(), setFocus(), and update() ...

Any help appreciated.

jacek
10th November 2008, 20:49
Do you invoke the base class implementation? Doesn't text drag and drop work by default?

Workeml
10th November 2008, 21:18
Hi Jacek,

That's the problem: if I invoke the base class QTextEdit::dropEvent(), then the cursor is repainted properly, but I'm trying to override the base class method.

What I'm trying to do is subclass QTextEdit to be able to have image icons within text. The image icons are to point back to some data. I do this with QTextImageFormat::setProperty(). But when the icons get selected and drag and dropped, the QTextEdit::dropEvent() wipes out that associated information.

If it weren't for that, I'd just use the default drag-and-drop.

As a workaround I've also tried calling QDropEvent::setDropAction(Qt::CopyAction) before calling the base class QTextEdit::dropEvent() - with the intention of copying the QTextImageFormat property over to the copy and then deleting the drag source - but the QTextEdit::dropEvent() seems to still do a move instead of a copy, deleting the source information ...

jacek
10th November 2008, 22:18
Have you tried the createMimeDataFromSelection() and insertFromMimeData() methods instead? QTextEdit inherits QScrollArea, so its drop event works a bit differently.

Workeml
11th November 2008, 00:04
Thanks for the suggestion. I hadn't tried them yet, but from a quick test, they don't seem to retain the user property of the QTextImageFormat icons that are drag and dropped. I may have to write some mime data handling or find some other solution.

chris_helloworld
20th October 2010, 16:08
Hi.
I know this threads a bit ancient, but was a solution found ?

I have a similar problem. I'm overriding QTextEdit::dropEvent to handle drops of objects dragged. I need to construct the text to be inserted based on the mimedata being dragged. For example I may want to drop a file on the text edit and insert the files filename. I then simply call insertPlainText(myString) which inserts the text as expected.

I then encounter the same problem with the cursor not being correctly drawn afterwards.

The only workaround I've found is to call setText(myString) on the QMimeData object, (using a const_cast), and then calling the QTextEdit::dropEvent(event) to handle the insertion. This works but seems like a hack and surely isn't the right thing to do.

Any suggestions ?
Cheers,
Chris.

baohaojun
5th May 2017, 16:47
Hi.

Now this is really old, but I've hit the same problem, and have found a solution:

this->setReadOnly(true);
QTextEdit::dropEvent(ev);
this->setReadOnly(false);

mohamed469
14th September 2021, 07:43
I have the same problem and I don't understand what this code does, can you explain more

mohamed469
15th September 2021, 06:35
I solved the problem by calling the default dropEvent function and pass the current event to it
like this:


void myTextEdit::dropEvent(QDropEvent *event)
{
if(event->mimeData()->hasText())
QTextEdit::dropEvent(event);
}