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
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
- #ifndef GVIEW_H
- #define GVIEW_H
- #include <QWidget>
- #include <QGraphicsView>
- QT_BEGIN_NAMESPACE
- class QMimeData;
- QT_END_NAMESPACE
- {
- Q_OBJECT
- public:
- public slots:
- void clear();
- signals:
- protected:
- };
- #endif // GVIEW_H
//gview.cpp
- #include "gview.h"
- #include <QtGui>
- {
- setMinimumSize(200, 200);
- setAcceptDrops(true);
- setAutoFillBackground(true);
- //s clear();
- }
- {
- //s setText(tr("<drop content>"));
- event->acceptProposedAction();
- emit changed(event->mimeData());
- }
- {
- event->acceptProposedAction();
- }
- {
- if (mimeData->hasImage()) {
- //s setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
- } else if (mimeData->hasHtml()) {
- //s setText(mimeData->html());
- //s setTextFormat(Qt::RichText);
- } else if (mimeData->hasText()) {
- //s setText(mimeData->text());
- //s setTextFormat(Qt::PlainText);
- } else if (mimeData->hasUrls()) {
- QString text;
- for (int i = 0; i < urlList.size() && i < 32; ++i) {
- }
- //s setText(text);
- } else {
- //s setText(tr("Cannot display data"));
- }
- event->acceptProposedAction();
- }
- {
- //s clear();
- event->accept();
- }
- void gview::clear()
- {
- // setText(tr("<drop content>"));
- emit changed();
- }
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.
You must log in to post a reply. Not a member yet? Register here!
