Results 1 to 6 of 6

Thread: Drag and drop QTreeWidgetItem to QTableWidget

  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Drag and drop QTreeWidgetItem to QTableWidget

    Hi

    I'm trying to drag a QTreeWidgetItem from a custom QTreeWidget into a QTableWidget.
    My problem is that i always have the forbiden cursor when I'm on the QTableWidget, and I never received the "drop" event!

    This is the code in my custom QTreeWidget
    Qt Code:
    1. void QTreeWidgetDragDrop::mousePressEvent(QMouseEvent *event)
    2. {
    3. // Get current selection
    4. QTreeWidgetItem *selectedItem = currentItem();
    5.  
    6. // If the selected Item exists
    7. if (selectedItem)
    8. {
    9. // Create data
    10. QMimeData *mimeData = new QMimeData();
    11. mimeData->setText("hello");
    12.  
    13. // Create drag
    14. QDrag *drag = new QDrag(this);
    15. drag->setMimeData(mimeData);
    16.  
    17. drag->exec(Qt::CopyAction);
    18. }
    19.  
    20. QTreeWidget::mousePressEvent(event);
    21. }
    To copy to clipboard, switch view to plain text mode 

    and this is the code I have in my custom QTableWidget
    Qt Code:
    1. void QTableWidgetDragDrop::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. qDebug() << "dragEnterEvent text: " << event->mimeData()->text();
    4.  
    5. // Allways accept at first
    6. event->acceptProposedAction();
    7.  
    8. QTableWidget::dragEnterEvent(event);
    9. }
    To copy to clipboard, switch view to plain text mode 

    And this is the function to catch the drop event
    Qt Code:
    1. void QTableWidgetDragDrop::dropEvent(QDropEvent *event)
    2. {
    3. qDebug() << "enter in dropEvent";
    4. }
    To copy to clipboard, switch view to plain text mode 

    I cannot drop my item because I have the forbiden cursor, I never see the sentence "enter in dropEvent" but I have in output
    dragEnterEvent text: "hello"
    In addition, I have put in my QTableWidget constructor:
    Qt Code:
    1. this->setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 

    Any idea ...?

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and drop QTreeWidgetItem to QTableWidget

    try to reimplement

    Qt Code:
    1. QTableWidgetDragDrop::dragMoveEvent(...)
    To copy to clipboard, switch view to plain text mode 

    and accept event here too

  3. #3
    Join Date
    Oct 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and drop QTreeWidgetItem to QTableWidget

    can you try it in debug mode and check all events?
    Last edited by ernie; 10th October 2010 at 14:53.

  4. #4
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and drop QTreeWidgetItem to QTableWidget

    I have already implemented dragMoveEvent, but I don't receive the event too...
    I'll try to check events in debug mode, I have to learn how the debug mode works.

  5. #5
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and drop QTreeWidgetItem to QTableWidget

    The call of "QAbstractItemView::dragEnterEvent" (in the mother class) contains
    Qt Code:
    1. if (d_func()->canDecode(event)) {
    2. event->accept();
    3. setState(DraggingState);
    4. } else {
    5. event->ignore();
    6. }
    To copy to clipboard, switch view to plain text mode 

    But, I saw in debug that it always goes in "event->ignore();", I have to work on that point ...

    => http://doc.trolltech.com/4.2/model-v...bclassing.html part "Drag and Drop Support and MIME Type Handling"
    Last edited by RomainGallard; 11th October 2010 at 12:38.

  6. #6
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and drop QTreeWidgetItem to QTableWidget

    I figured out!
    My problem was what I found: I didn't set a model, and I didn't put my data into the default model!
    So I put my data into the default model, and now I can see all my events!
    My next step is to create my own model!

    (You can put this thread as resolved )

    Thanks for your help


    Qt Code:
    1. void QTreeWidgetDragDrop::mousePressEvent(QMouseEvent *event)
    2. {
    3. // Get current selection
    4. QTreeWidgetItem *selectedItem = currentItem();
    5.  
    6. // If the selected Item exists
    7. if (selectedItem)
    8. {
    9. QByteArray itemData;
    10. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    11. dataStream << "Hello";
    12.  
    13. QMimeData *mimeData = new QMimeData;
    14. mimeData->setData("application/x-qabstractitemmodeldatalist", itemData);
    15.  
    16. // Create drag
    17. QDrag *drag = new QDrag(this);
    18. drag->setMimeData(mimeData);
    19.  
    20.  
    21. qDebug() << "mousePressEvent before exec";
    22. drag->exec(Qt::CopyAction);
    23. qDebug() << "mousePressEvent after exec";
    24. }
    25.  
    26. QTreeWidget::mousePressEvent(event);
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Drag & Drop rows in a QTableWidget
    By vycke in forum Qt Programming
    Replies: 7
    Last Post: 19th January 2012, 00:01
  2. Drag and Drop QTableWidget in UI file.
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 23:02
  3. Replies: 3
    Last Post: 26th April 2008, 18:42
  4. Drag & Drop using QTableWidget
    By Israa in forum Qt Programming
    Replies: 21
    Last Post: 12th April 2007, 19:35
  5. Drag & Drop QTreeWidgetItem with editors
    By Mad Max in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2007, 09:09

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.