Results 1 to 4 of 4

Thread: Enable Drag & Drop between QTableWidget and QLineEdit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Lightbulb Re: Enable Drag & Drop between QTableWidget and QLineEdit

    I think I found a solution to the problem.

    The QTableWidget uses a special MIME type which can't be used by other controls/applications but there is a way to add additional MIME types by overwriting the following methods:

    Qt Code:
    1. bool DragAndDropTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
    2. {
    3. if(data->hasText())
    4. {
    5. QTableWidgetItem *item = this->item(row, column);
    6. if(item != 0)
    7. item->setText(data->text());
    8. return false;
    9. }
    10. else
    11. {
    12. return QTableWidget::dropMimeData(row, column, data, action);
    13. }
    14. }
    15.  
    16. QStringList DragAndDropTableWidget::mimeTypes() const
    17. {
    18. return QTableWidget::mimeTypes() << QStringLiteral("text/plain");
    19. }
    20.  
    21. QMimeData *DragAndDropTableWidget::mimeData(const QList<QTableWidgetItem *> items) const
    22. {
    23. QString text;
    24. if(items.count() > 0)
    25. {
    26. QTableWidgetItem *lastItem = items.last();
    27. foreach(QTableWidgetItem *item, items)
    28. {
    29. if(!item->text().isEmpty())
    30. {
    31. text.append(item->text());
    32. if(item != lastItem)
    33. {
    34. text.append(QStringLiteral("\n"));
    35. }
    36. }
    37. }
    38. }
    39. QMimeData *data = QTableWidget::mimeData(items);
    40. data->setText(text);
    41.  
    42. return data;
    43. }
    To copy to clipboard, switch view to plain text mode 
    I'm adding the MIME type "text/plain" which is used by most applications and it seems to work (even if there are multiple cells selected).
    There's no use in overwriting dropEvent, dragMoveEvent and dragEnterEvent.

    If someone has suggestions to improve the code, let me know because I'm not sure how to deal with that methods due I found no examples about them and the documentation doesn't give a lot of information, too.
    Last edited by Infinity; 3rd April 2013 at 16:49.

Similar Threads

  1. How to customize Drag and Drop for QTableWidget?
    By linxs in forum Qt Programming
    Replies: 0
    Last Post: 17th November 2012, 16:56
  2. Drag & Drop rows in a QTableWidget
    By vycke in forum Qt Programming
    Replies: 7
    Last Post: 19th January 2012, 00:01
  3. Drag and drop reordering in a QTableWidget
    By Lendrick in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2009, 23:19
  4. Drag and Drop QTableWidget and QTableView
    By scorpion11 in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 09:33
  5. Drag & Drop using QTableWidget
    By Israa in forum Qt Programming
    Replies: 21
    Last Post: 12th April 2007, 19:35

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
  •  
Qt is a trademark of The Qt Company.