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?
-
hi. durgeshK.
i tryed. QGraphicsView and drag & drop. in qt src.
this code is very simple.
C:\Qt\2010.05\qt\examples\draganddrop\dropsiteand. 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_NAMESPACEclass gview : public QGraphicsView
{
Q_OBJECT
public:
explicit gview(QWidget *parent = 0);public slots:
void clear();signals:
void changed(const QMimeData *mimeData = 0);protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dropEvent(QDropEvent *event);
};#endif // GVIEW_H
@//gview.cpp
@
#include "gview.h"#include <QtGui>
gview::gview(QWidget *parent)
: QGraphicsView(parent)
{
setMinimumSize(200, 200);
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setAlignment(Qt::AlignCenter);
setAcceptDrops(true);
setAutoFillBackground(true);
//s clear();
}void gview::dragEnterEvent(QDragEnterEvent *event)
{
//s setText(tr("<drop content>"));
setBackgroundRole(QPalette::Highlight);event->acceptProposedAction(); emit changed(event->mimeData());
}
void gview::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}void gview::dropEvent(QDropEvent *event)
{
const QMimeData *mimeData = event->mimeData();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()) {
QList<QUrl> urlList = mimeData->urls();
QString text;
for (int i = 0; i < urlList.size() && i < 32; ++i) {
QString url = urlList.at(i).path();
text += url + QString("\n");
}
//s setText(text);
} else {
//s setText(tr("Cannot display data"));
}
setBackgroundRole(QPalette::Dark);
event->acceptProposedAction();
}void gview::dragLeaveEvent(QDragLeaveEvent *event)
{
//s clear();
event->accept();
}void gview::clear()
{
// setText(tr("<drop content>"));
setBackgroundRole(QPalette::Dark);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.
-
-
Hi,
I know this is old post, but i tried to implement this in my application. I am not able to correctly do get the display. Though i am not getting any error. if i enable a
@ QGraphicsPixmapItem(item) or
GraphicsPixmapItem *itemitem->setPixmap(.....); @
But the image is not getting displayed in my QGraphicsView created by the QTDesigner. Would love to have your suggestions. Should i add any code to my widget.cpp to connect the gview.h other than steps mentioned here.
Regards
Venkatesh