PDA

View Full Version : QGraphicsTextItem subclass to provide insertFromMimeData-type functionality



adamb924
18th December 2011, 05:56
Hello,

For my application I would like to have QGraphicsTextItem that only allows plain text to be inserted (pasted or dropped). Could anyone suggest a way of doing this?

The insertFromMimeData method of QTextEdit would be ideal. I have had a look at the source, but I can't figure out how to implement that something in a subclass of QGraphicsTextItem.

The dropEvent and inputMethodEvent methods of QGraphicsTextItem don't seem to allow me to do this either, though it's entirely possible I'm not using them the right way.

Thanks for any thoughts,
Adam

stampede
18th December 2011, 11:49
For the "paste" functionality, you can reimplement keyPressEvent and check for the "Ctrl+V" combination.
About the dropEvent, have you enabled the item to accept drops, with item->setAcceptDrops(true) ?

amleto
18th December 2011, 11:52
show us what you have tried, with a compilable example. Otherwise we have to guess at what you may have done wrong.

adamb924
19th December 2011, 05:38
For the "paste" functionality, you can reimplement keyPressEvent and check for the "Ctrl+V" combination.
About the dropEvent, have you enabled the item to accept drops, with item->setAcceptDrops(true) ?

Thanks, stampede.

Taking another look at this, I was able to modify the MIME data in these events by creating a pointer to a new QMimeData object, rather than passing the address of a local QMimeData object. Perhaps it should have been obvious.

On capturing the key event, I assume the suggestion is to modify the system keyboard. I have done this and it works. I wonder, in general, is it considered bad form for an application for modify the clipboard?

One other little change I made was to test for the paste command with event->matches( QKeySequence::Paste ), which allows for internationalized shortcuts.

Thanks,
Adam

stampede
19th December 2011, 11:19
I wonder, in general, is it considered bad form for an application for modify the clipboard?
I don't think so, in fact that's the whole purpose of the clipboard, providing temporary storage and data transfer for all applications. When I press the "copy shortcut", then I'm aware that previous content of the clipboard will be erased, so IMHO thats sensible behavior.