February 1, 2011

durgeshK durgeshK
Lab Rat
28 posts

DropEvent is not getting called for QGraphicsScene

 

Hello everyone,

I have sub classed QGraphicsScene to implement the Drag and Drop feature. Now when i start a drag event form another widget(another QGraphicsView), all events related to dragging, i.e., dragEnterEvent, dragMoveEvent and dragLeaveEvent are getting called, but dropEvent is not getting called.

Any Suggestion?

3 replies

February 1, 2011

shint shint
Lab Rat
25 posts

hi. durgeshK.
i tryed. QGraphicsView and drag & drop. in qt src.
this code is very simple.
C:\Qt\2010.05\qt\examples\draganddrop\dropsite

and. i received drop event.

//code.
//————————————————————————————-
<step 1> create class – i make gview.cpp gview.h (QWidget)
//————————————————————————————-
<step 2> i make ui. (use Qt designer)
//————————————————————————————-

  • create QGraphicsView.
  • mouse right click – Promoted widgets
  • base QGraphicsView
  • class gview [add btn click]
  • [Promoted click]

//————————————————————————————-
<step 3> copy & paste this code.
//————————————————————————————-
//gview.h

  1. #ifndef GVIEW_H
  2. #define GVIEW_H
  3.  
  4. #include <QWidget>
  5. #include <QGraphicsView>
  6.  
  7.  
  8. QT_BEGIN_NAMESPACE
  9. class QMimeData;
  10. QT_END_NAMESPACE
  11.  
  12.  
  13. class gview : public QGraphicsView
  14. {
  15.     Q_OBJECT
  16. public:
  17.     explicit gview(QWidget *parent = 0);
  18.  
  19. public slots:
  20.     void clear();
  21.  
  22. signals:
  23.     void changed(const QMimeData *mimeData = 0);
  24.  
  25. protected:
  26.     void dragEnterEvent(QDragEnterEvent *event);
  27.     void dragMoveEvent(QDragMoveEvent *event);
  28.     void dragLeaveEvent(QDragLeaveEvent *event);
  29.     void dropEvent(QDropEvent *event);
  30. };
  31.  
  32. #endif // GVIEW_H

//gview.cpp

  1. #include "gview.h"
  2.  
  3. #include <QtGui>
  4.  
  5.  
  6. gview::gview(QWidget *parent)
  7.     : QGraphicsView(parent)
  8. {
  9.     setMinimumSize(200, 200);
  10.     setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
  11.     setAlignment(Qt::AlignCenter);
  12.     setAcceptDrops(true);
  13.     setAutoFillBackground(true);
  14. //s    clear();
  15. }
  16.  
  17. void gview::dragEnterEvent(QDragEnterEvent *event)
  18. {
  19. //s    setText(tr("<drop content>"));
  20.     setBackgroundRole(QPalette::Highlight);
  21.  
  22.     event->acceptProposedAction();
  23.     emit changed(event->mimeData());
  24. }
  25.  
  26. void gview::dragMoveEvent(QDragMoveEvent *event)
  27. {
  28.     event->acceptProposedAction();
  29. }
  30.  
  31. void gview::dropEvent(QDropEvent *event)
  32. {
  33.     const QMimeData *mimeData = event->mimeData();
  34.  
  35.     if (mimeData->hasImage()) {
  36. //s        setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
  37.     } else if (mimeData->hasHtml()) {
  38. //s        setText(mimeData->html());
  39. //s        setTextFormat(Qt::RichText);
  40.     } else if (mimeData->hasText()) {
  41. //s        setText(mimeData->text());
  42. //s        setTextFormat(Qt::PlainText);
  43.     } else if (mimeData->hasUrls()) {
  44.         QList<QUrl> urlList = mimeData->urls();
  45.         QString text;
  46.         for (int i = 0; i < urlList.size() && i < 32; ++i) {
  47.             QString url = urlList.at(i).path();
  48.             text += url + QString("\n");
  49.         }
  50. //s        setText(text);
  51.     } else {
  52. //s        setText(tr("Cannot display data"));
  53.     }
  54.     setBackgroundRole(QPalette::Dark);
  55.     event->acceptProposedAction();
  56. }
  57.  
  58.  
  59. void gview::dragLeaveEvent(QDragLeaveEvent *event)
  60. {
  61. //s    clear();
  62.     event->accept();
  63. }
  64.  
  65.  
  66. void gview::clear()
  67. {
  68. //    setText(tr("<drop content>"));
  69.     setBackgroundRole(QPalette::Dark);
  70.  
  71.     emit changed();
  72. }

February 2, 2011

durgeshK durgeshK
Lab Rat
28 posts

Hi Shint,

Thanks for the code. But again i am facing the same problem, even your code is working fine for drags from outside the application, but dropEvent is again not being called if a drag is initiated from within the application.

I have two graphicsView, the one acting as a source and other as a destination. Here everything is working fine except that the dropEvent is not being called when i release the mouse.

February 2, 2011

durgeshK durgeshK
Lab Rat
28 posts

Hi again Shint,

Your code is working fine, the problem was at my end while executing the drag.
Earlier i was using @drag->exec(Qt::IgnoreAction)@, so the dropEvent call was ignored. Now when i am using @drag->exec()@, everything works fine.(Even my code :) ).

Thanks & Regards,
Durgesh

 
  ‹‹ Problem when inserting html      Segmentation fault when using static SQL ››

You must log in to post a reply. Not a member yet? Register here!