Results 1 to 4 of 4

Thread: Enable Drag & Drop between QTableWidget and QLineEdit

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

    Question Enable Drag & Drop between QTableWidget and QLineEdit

    Hi

    I have a QTableWidget and I want to enable Drag & Drop functionality. For that I'm using the following code:

    Qt Code:
    1. tableWidget->setDragEnabled(true);
    2. tableWidget->setDragDropOverwriteMode(true);
    3. tableWidget->setDragDropMode(QAbstractItemView::DragDrop);
    4. tableWidget->setDefaultDropAction(Qt::CopyAction);
    To copy to clipboard, switch view to plain text mode 

    The Drag & Drop functionality works, but only between other QTableWidgets. It doesn't work between a QTableWidget and a QLineEdit. Is it possible to enable Drag & Drop between QTableWidgets and QLineEdits, too?
    Has someone an idea to solve the problem?

    EDIT: I already tried overwriting dropEvent, dragMoveEvent and dragEnterEvent. I'm not sure what to do, but simply calling event->acceptProposedAction(); or calling event->accept(); doesn't work.

    If I use the following code and try do drop some text from any text field (e. g. the address bar in my browser) my application terminates (segmentation fault).
    Qt Code:
    1. void DragAndDropTableWidget::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. event->acceptProposedAction();
    4. }
    To copy to clipboard, switch view to plain text mode 

    (I'm using the Linux Version of Qt 5.0.1)
    Last edited by Infinity; 31st March 2013 at 22:14.

  2. #2
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Enable Drag & Drop between QTableWidget and QLineEdit

    Prob because your trying to drop an item into a line edit try drooping the item->text()

  3. #3
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Enable Drag & Drop between QTableWidget and QLineEdit

    I'm not sure what you mean. Can you explain this a bit more detailed. But I'm not trying to drop data into a line edit - I'm trying to drop data from the line edit into a cell. (Both is not working, but the program terminates only in this case.)

    Qt Code:
    1. void DragAndDropTableWidget::dropEvent(QDropEvent *event)
    2. {
    3. QTableWidget::dropEvent(event);
    4. }
    5.  
    6. void DragAndDropTableWidget::dragMoveEvent(QDragMoveEvent *event)
    7. {
    8. QTableWidget::dragMoveEvent(event);
    9. }
    10.  
    11. void DragAndDropTableWidget::dragEnterEvent(QDragEnterEvent *event)
    12. {
    13. QTableWidget::dragEnterEvent(event);
    14. }
    15.  
    16. bool DragAndDropTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
    17. {
    18. return QTableWidget::dropMimeData(row, column, data, action);
    19. }
    To copy to clipboard, switch view to plain text mode 
    I set in each method a break point to check out if or when these functions are getting called. These functions only get called when I drag & drop between QTableWidgets so I think nothing can be achieved by overwriting these methods.

    This is likely not related to my problem but I also noticed that Spotify (which also uses Qt, at least the Linux version) terminates if you try to drag & drop songs.
    Last edited by Infinity; 1st April 2013 at 19:12.

  4. #4
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 20 Times in 20 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 17: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, 17:56
  2. Drag & Drop rows in a QTableWidget
    By vycke in forum Qt Programming
    Replies: 7
    Last Post: 19th January 2012, 01:01
  3. Drag and drop reordering in a QTableWidget
    By Lendrick in forum Qt Programming
    Replies: 1
    Last Post: 23rd November 2009, 00:19
  4. Drag and Drop QTableWidget and QTableView
    By scorpion11 in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 10:33
  5. Drag & Drop using QTableWidget
    By Israa in forum Qt Programming
    Replies: 21
    Last Post: 12th April 2007, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.