Results 1 to 5 of 5

Thread: QGraphicsTextItem subclass to provide insertFromMimeData-type functionality

  1. #1
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsTextItem subclass to provide insertFromMimeData-type functionality

    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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsTextItem subclass to provide insertFromMimeData-type functionality

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

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsTextItem subclass to provide insertFromMimeData-type functionality

    show us what you have tried, with a compilable example. Otherwise we have to guess at what you may have done wrong.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsTextItem subclass to provide insertFromMimeData-type functionality

    Quote Originally Posted by stampede View Post
    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

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsTextItem subclass to provide insertFromMimeData-type functionality

    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.

Similar Threads

  1. Does Qt have 'search as you type' functionality?
    By bifftanner in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2011, 01:08
  2. Replies: 5
    Last Post: 15th August 2011, 21:34
  3. Replies: 51
    Last Post: 26th November 2010, 13:24
  4. QGraphicsTextItem subclass & QGraphicsScene
    By ttvo in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2009, 15:46
  5. QTextBrowser paste insertFromMimeData(mimex)
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 12:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.