Results 1 to 2 of 2

Thread: Drag and drop image into a QTextEdit ?

  1. #1
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Wink Drag and drop image into a QTextEdit ?

    I am trying to be able to drag and drop images from files into a QTextEdit and display them in it.
    I subclassed QTextEdit reimplemented dropEvent and it sort of works, the right url of image is used, when I open the resulting document(saved from the QTextEdit in html) in firefox the image gets displayed, but in my QTextEdit only an image icon is displayed, not the image.
    Also after I drag and drop the image the cursor stops blinking.

    Here is the code:

    Qt Code:
    1. void CQTextEdit::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. event->acceptProposedAction();
    4. }
    5.  
    6. void CQTextEdit::dropEvent(QDropEvent *e)
    7. {
    8. if (e->mimeData()->hasFormat("text/uri-list"))
    9. {
    10. journalImage = &qvariant_cast<QImage>(e->mimeData()->imageData());
    11. QTextCursor cursor = this->textCursor();
    12. QTextDocument *document = this->document();
    13. cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,this->toPlainText().size());
    14. document->addResource(QTextDocument::ImageResource,e->mimeData()->urls()[0],journalImage);
    15. cursor.insertImage(e->mimeData()->urls()[0].toString());
    16. this->setHtml(this->toHtml());
    17.  
    18. }
    19. else QTextEdit::dropEvent(e);
    20. }
    To copy to clipboard, switch view to plain text mode 

    journalImage is a pointer to a QImage.
    Part of the code was inspired from the docs:
    http://doc.trolltech.com/4.3/qtextedit.html
    Which I also tried to use, but with not too much luck.
    Can anyone help me ?
    May the source be with you!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag and drop image into a QTextEdit ?

    The key to success seems to be reimplementing QTextEdit::canInsertFromMimeData() and QTextEdit::insertFromMimeData(). See wiki: Drop images on QTextEdit for more details.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    balazsbela (3rd September 2007)

Similar Threads

  1. Replies: 7
    Last Post: 8th September 2006, 17:19
  2. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 16:37
  3. drag and drop
    By christopher in forum Newbie
    Replies: 1
    Last Post: 19th August 2006, 21:59
  4. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 17:41
  5. drop image fly back
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 8th March 2006, 13:00

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.