Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is it possible to do a drag and drop on an image to a rectangle?

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

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    franziss
    wrote on last edited by
    #1

    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. =)

    1 Reply Last reply
    0
    • F Offline
      F Offline
      franziss
      wrote on last edited by
      #2

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

      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved