Erfan Tavakoli
Erfan Tavakoli

Reputation: 356

Qt: qgraphicsitem drag and drop

I have a problem with Qt. I want to drag an image and use of QGraphicsPixmapItem and dragenterevent but it doesnt appear in the console ? but other function like hoverEnterEvent work corectly??? here is the code: please help?

class button : public QGraphicsPixmapItem
{
public:
    button(const QPixmap &);
    button();
    void changepic( QPixmap,int ,int);
    void mousePressEvent(QGraphicsSceneMouseEvent*event);
    void dragenterEvent(QGraphicsSceneDragDropEvent *event){

        event->setAccepted(1);
       qDebug("drag");
    }
    void dropEvent(QGraphicsSceneDragDropEvent *event){
       qDebug("drop");
    }

    void hoverEnterEvent(QGraphicsSceneHoverEvent *event){

           // do something
       // this->setPos(this->x()+10,this->y()+10);

             qDebug("k");

           QGraphicsItem::hoverMoveEvent(event);
       }
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};

Upvotes: 3

Views: 8069

Answers (1)

Dave Mateer
Dave Mateer

Reputation: 17956

Are you just wanting to be able to move a graphics item around your scene? If so, then all you need to do is set some flags for the item. For example,

setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);

If you are actually trying to implement drag-and-drop onto your graphics item, you must call setAcceptDrops(true) for your item.

The reason for some of this extra work is that Qt, by default, is trying to optimize the performance of your scene. So you have to explicitly opt-in to some of these extra features. You are saying, "I know this will have a negative impact on performance, but I'm OK with that; I need this feature."

Upvotes: 12

Related Questions