Is it possible to do a drag and drop on an image to a rectangle?



  • I try to do a drag and drop on an image to a rectangle.

    I have an image whose id: icon
    @
    Image {
    id: icon
    width: 64
    height: 64
    source: "liverbird.gif"

           MouseArea {
               id: liverbirdMouseArea
               anchors.fill: parent
               width: 64
               height: 64
    
               drag.target: parent
               drag.axis: Drag.XandYAxis
               onReleased: {
                   cc.putItemIntoSlide(cubeFront, icon);
               }
    
           }
       }
    

    @

    and I try to put icon into a rectangle with id:cubeFront. So when I click icon, drag it to cubeFront, and release, I will go into the function putItemIntoSlide, which sets icon as a child of cubeFront.

    I check if icon is in cubeFront, by mapping the coordinates of icon to cubeFront, and then check if cubeFront contain this coordinates.

    @
    void cc::putItemIntoSlide(QGraphicsObject * slide, QGraphicsObject * object)
    {
    if (object != NULL)
    {
    if (slide->contains((slide->mapFromItem(object,object->pos()).toPoint())))
    {
    qDebug("got object in slide");
    object->setParentItem(slide);
    }
    }
    else
    {
    qDebug("no object");
    }
    }

    @

    But this doesn't work. Any suggestions? Much appreciated. =)



  • I found the solution! This is an easy way to drag and drop in qt

    @
    QGraphicsItem *oldParent = object->parentItem();
    if (slide->contains((slide->mapFromItem(oldParent,object->pos()))))
    {
    qDebug("got object in slide");
    object->setParentItem(slide);
    object->setPos(slide->mapFromItem(oldParent,object->pos()));
    }
    else
    {
    object->setParentItem(0);
    }

    @


Log in to reply
 

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