Results 1 to 2 of 2

Thread: Drag an drop between differents QTableWidget...

  1. #1
    Join Date
    May 2008
    Location
    Spain
    Posts
    90
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Drag an drop between differents QTableWidget...

    Hello,

    I have some problems with drag and drop between differents QTableWigdet.
    Table A is source information, where I selected and drag the entire row but the last cell.

    snapshot1.jpg

    Now, I would like to drop this information in Table B. (botton QTableWidget).
    I have implemented dragEnterEvent, dragMoveEvent, dropEvent and dragLeaveEvent and it works fine.

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3. #include "LeiTableWidget.h"
    4.  
    5. LeiTableWidget::LeiTableWidget(QWidget *parent)
    6. : QTableWidget(parent)
    7. {
    8. setAcceptDrops(true);
    9. setAutoFillBackground(true);
    10. clear();
    11. }
    12.  
    13. void LeiTableWidget::dragEnterEvent(QDragEnterEvent *event)
    14. {
    15. // qDebug() << "dragEnterEvent";
    16. event->acceptProposedAction();
    17. }
    18.  
    19. void LeiTableWidget::dragMoveEvent(QDragMoveEvent *event)
    20. {
    21. // qDebug() << "dragMoveEvent";
    22. event->acceptProposedAction();
    23. }
    24.  
    25. void LeiTableWidget::dropEvent(QDropEvent *event)
    26. {
    27. qDebug() << "dropEvent";
    28.  
    29. QTableWidget* table = qobject_cast<QTableWidget*>(event->source());
    30.  
    31. QPoint old_coordinates = QPoint(-1,-1);
    32. if(table->currentItem() != NULL)
    33. {
    34. // Tengo las coordenadas del elemento que se ha arrastrado desde su origen
    35. //**************************************************************************
    36. old_coordinates = QPoint( table->currentItem()->row(), table->currentItem()->column() );
    37. }
    38.  
    39. qDebug() << table->currentItem()->row() << "-" << table->currentItem()->column() ;
    40.  
    41. qDebug() << "mine data " << event->mimeData()->text();
    42. QTableWidget::dropEvent(event);
    43. event->acceptProposedAction();
    44. }
    45.  
    46. void LeiTableWidget::dragLeaveEvent(QDragLeaveEvent *event)
    47. {
    48. // qDebug() << "dragLeaveEvent";
    49. event->accept();
    50. }
    51.  
    52. void LeiTableWidget::clear()
    53. {
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 
    but now I have some problems:

    1.- If a drop in a cell different than column 0, write incorrect data in table.
    snapshot2.jpg

    2.- How can I detect what information or items has drop action?
    3.- How can I detect what row is select to drop?

    Best regards.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Drag an drop between differents QTableWidget...

    Try implementing dropMimeData instead of dropEvent

    See if this works

    Qt Code:
    1. bool LeiTableWidget::dropMimeData (int row, int column, const QMimeData * data, Qt::DropAction action)
    2. {
    3. return QTableWidget::dropMimeData (row, 0, data, action);
    4. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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 QTreeWidgetItem to QTableWidget
    By RomainGallard in forum Qt Programming
    Replies: 5
    Last Post: 11th October 2010, 14:02
  4. Drag and Drop QTableWidget in UI file.
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 21st January 2009, 00:02
  5. Drag & Drop using QTableWidget
    By Israa in forum Qt Programming
    Replies: 21
    Last Post: 12th April 2007, 20:35

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.