Results 1 to 6 of 6

Thread: QTextEdit Drag and Drop

  1. #1
    Join Date
    Mar 2010
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextEdit Drag and Drop

    First step was:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. textEdit = new QTextEdit;
    4. setCentralWidget(textEdit);
    5.  
    6. textEdit->setAcceptDrops(false);
    7. setAcceptDrops(true);
    8.  
    9. setWindowTitle(tr("Text Editor"));
    10. }
    11.  
    12. void MainWindow::dragEnterEvent(QDragEnterEvent *event)
    13. {
    14. event->accept();
    15. }
    16.  
    17. void MainWindow::dragMoveEvent(QDragMoveEvent *event)
    18. {
    19. event->accept();
    20. }
    21.  
    22. void MainWindow::dropEvent(QDropEvent *event)
    23. {
    24. if (event->mimeData()->hasFormat("text/uri-list")) {
    25. QList<QUrl> urls = event->mimeData()->urls();
    26. if (urls.isEmpty())
    27. return;
    28.  
    29. QString fileName = urls.first().toLocalFile();
    30. if (fileName.isEmpty())
    31. return;
    32.  
    33. if (readFile(fileName))
    34. setWindowTitle(tr("%1 - %2").arg(fileName)
    35. .arg(tr("Drag File")));
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 
    In the second step I enabled drops for textEdit
    Qt Code:
    1. textEdit->setAcceptDrops(true)
    To copy to clipboard, switch view to plain text mode 
    and subclassed it, so now it's possible to move/copy a text from a line to another:
    Qt Code:
    1. ...dragEnterEvent(QDragEnterEvent *e)
    2. {
    3. if (e->source()) {
    4. if (e->source()->objectName() == "qt_scrollarea_viewport") {
    5. e->accept();
    6. }
    7. }
    8. }
    9.  
    10. ...dragMoveEvent(QDragMoveEvent *e)
    11. {
    12. e->accept();
    13. }
    To copy to clipboard, switch view to plain text mode 
    My questions:
    1. During drag the cursor stays hidden: is this due to the non-propagation of the event to the parent?
    2. Why subclassing dropEvent() like this
    Qt Code:
    1. ...dropEvent(QDropEvent *e)
    2. {
    3. e->accept();
    4. }
    To copy to clipboard, switch view to plain text mode 
    text is not copied/moved?
    3. Are there any alternatives to get this task?
    I hope this thread will be useful for others.
    Thank you so much.

  2. #2
    Join Date
    Mar 2010
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QTextEdit Drag and Drop

    4. Why
    Qt Code:
    1. ...dragEnterEvent(QDragEnterEvent *e)
    2. {
    3. if (e->source() == this)
    4. e->accept();
    5. }
    To copy to clipboard, switch view to plain text mode 
    does not work?

  3. #3
    Join Date
    Mar 2010
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit Drag and Drop

    Unclear questions?

  4. #4
    Join Date
    Mar 2010
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit Drag and Drop

    5. Why
    Qt Code:
    1. ...dropEvent(QDropEvent *e)
    2. {
    3. const QMimeData *mimeData = e->mimeData();
    4. insertFromMimeData(mimeData);
    5. }
    To copy to clipboard, switch view to plain text mode 
    this neither?

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit Drag and Drop

    Please don't ask questions in visitor messages. If someone can answer your post, they will.

  6. #6
    Join Date
    Mar 2010
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit Drag and Drop

    Thank you for your kind specification and for the precious help.

Similar Threads

  1. Replies: 3
    Last Post: 10th June 2010, 15:13
  2. Replies: 0
    Last Post: 4th May 2010, 10:24
  3. Drag Drop Images from WebPage into QTextEdit
    By Nemo in forum Qt Programming
    Replies: 7
    Last Post: 15th December 2009, 09:35
  4. QTextEdit drag and drop
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2008, 16:41
  5. Drag and drop image into a QTextEdit ?
    By balazsbela in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2007, 14:47

Tags for this Thread

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.