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.

    and. i received drop event.

    <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.
    #ifndef GVIEW_H
    #define GVIEW_H

    #include <QWidget>
    #include <QGraphicsView>

    class QMimeData;

    class gview : public QGraphicsView
    explicit gview(QWidget *parent = 0);

    public slots:
    void clear();

    void changed(const QMimeData *mimeData = 0);

    void dragEnterEvent(QDragEnterEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void dragLeaveEvent(QDragLeaveEvent *event);
    void dropEvent(QDropEvent *event);

    #endif // GVIEW_H

    #include "gview.h"

    #include <QtGui>

    gview::gview(QWidget *parent)
    : QGraphicsView(parent)
    setMinimumSize(200, 200);
    setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
    //s clear();

    void gview::dragEnterEvent(QDragEnterEvent *event)
    //s setText(tr("<drop content>"));

    emit changed(event->mimeData());


    void gview::dragMoveEvent(QDragMoveEvent *event)

    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"));

    void gview::dragLeaveEvent(QDragLeaveEvent *event)
    //s clear();

    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.

  • 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,

  • 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 *item

    item->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.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.