Results 1 to 3 of 3

Thread: Drop not working correctly

  1. #1
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Drop not working correctly

    Hello! Im dragging from a QTreeWidget to QGraphicsScene. And drop action doesnt occur.

    This code for drag:
    Qt Code:
    1. void MyTreeWidget::mousePressEvent(QMouseEvent* event)
    2. {
    3. if (event->button() == Qt::LeftButton)
    4. dragStartPosition = event->pos();
    5. QTreeWidget::mousePressEvent(event);
    6. }
    7.  
    8. void MyTreeWidget::mouseMoveEvent(QMouseEvent* event)
    9. {
    10. if (!(event->buttons() & Qt::LeftButton))
    11. return;
    12. if ((event->pos() - dragStartPosition).manhattanLength() < QApplication::startDragDistance())
    13. return;
    14. if(this->currentItem()->text(0) == QString::fromLocal8Bit("Text"))
    15. {
    16. QLabel* lb = new QLabel(QString::fromLocal8Bit("Text"));
    17. lb->setMaximumSize(100,20);
    18. lb->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
    19. QDrag *drag = new QDrag(this);
    20. drag->setPixmap(QPixmap::grabWidget(lb));
    21. QMimeData *mimeData = new QMimeData;
    22. drag->setMimeData(mimeData);
    23. Qt::DropAction dropAction = drag->exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    And this is for drop:

    Qt Code:
    1. MyGraphicsView::MyGraphicsView(QGraphicsScene* parent) : QGraphicsView(parent)
    2. {
    3. setAcceptDrops(true);
    4. }
    5.  
    6. void MyGraphicsView::dragEnterEvent(QDragEnterEvent* e)
    7. {
    8. cout << "drag entered" << endl;
    9. e->acceptProposedAction();
    10. }
    11.  
    12. void MyGraphicsView::dropEvent(QDropEvent *e)
    13. {
    14. cout << "dropped" << endl;
    15. e->acceptProposedAction();
    16. }
    To copy to clipboard, switch view to plain text mode 

    dragEnterEvent works correctly (at least cout in it). But dropEvent doesnt. And drop accepted symbol (+) isnt shown over pixmap im dragging.

    Help would be appreciated. Thanks in advance.
    Last edited by rippa; 11th November 2008 at 12:37.

  2. #2
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drop not working correctly

    I'm no expert and I've only used QTreeViews, not QTreeWidgets directly. It struck me that you don't set up any MimeFormats (or data) so the view won't know what's being dropped on it:

    Qt Code:
    1. ...
    2. QMimeData *mimeData = new QMimeData;
    3. //without the following line, my drag/drops with a QTreeView went badly wrong
    4. mimeData->setData(("application/x-something", QByteArray()); //replace x-something and QByteArray() if necessary
    5. drag->setMimeData(mimeData);
    6. ...
    To copy to clipboard, switch view to plain text mode 

    Just my ignorant thoughts - sorry if they're irrelevant!

  3. #3
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Re: Drop not working correctly

    Unfortunately, its not the point (. QMimeData instance should be specified fo QDrag to work, but i believe its not important is there any real information in this instance.
    Besides im passing some real info in my programm. I just cut those blocks from example code for the sake of simplicity.

    The problem is not that Drop drops something wrong, but that it doesnt even occur, like my accepting widget acceptDrops is FALSE.

Similar Threads

  1. Drag and Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 16
    Last Post: 19th May 2010, 14:57
  2. renderText is not working correctly
    By validator in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 16:55
  3. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  4. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41

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.