Qt: qgraphicsitem drag and drop

给你一囗甜甜゛ 提交于 2021-01-28 04:46:37

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/10950820/qt-qgraphicsitem-drag-and-drop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!