Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't get dragging and dropping to work
Qt 6.11 is out! See what's new in the release blog

Can't get dragging and dropping to work

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 2.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi everyone,

    I have been looking for over 4 hours into solving the issues I have with getting drag and drop to work. I tried almost every link I could find in google but I'm still clueless. I'm developping on openSuse 11.3 in eclipse with the qt integration plugin. Everything was going perfect until today. I'm using Qt 4.6.3 (if the package manager is right).

    The setup is as follows:
    I derived my own Scene class from QGraphicsView (it has some more transformations inside I need for my application). For this class I have set acceptDrops to true. The class overrides dragEnterEvent and dropEvent, they are completely trivial:
    @void Scene::dragEnterEvent (QDragEnterEvent* event) {
    event->acceptProposedAction();
    event->setAccepted(true);
    }

    void Scene::dropEvent (QDropEvent* event) {
    event->setAccepted(true);
    event->acceptProposedAction();
    }
    @

    On the other hand I have my own Point class that derives from QGraphicsItem. In that one I indicated:
    @ setFlag(QGraphicsItem::ItemIsMovable, true);
    @

    It overrides all necessary methods and also mousePressEvent, mouseMoveEvent and mouseReleaseEvent:
    @void InterpolationPoint::mousePressEvent(QGraphicsSceneMouseEvent * r)
    {
    setCursor(Qt::ClosedHandCursor);
    }

    void InterpolationPoint::mouseReleaseEvent(QGraphicsSceneMouseEvent * r)
    {
    setCursor(Qt::OpenHandCursor);
    }

    void InterpolationPoint::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
    if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
    .length() < QApplication::startDragDistance()) {
    return;
    }
    QDrag *drag = new QDrag(event->widget());
    QMimeData *mime = new QMimeData;
    drag->setMimeData(mime);

    mime->setColorData(QColor(0,0,0));
    

    QPixmap pixmap(4, 4);
    pixmap.fill(Qt::white);

    pixmap.setMask(pixmap.createHeuristicMask());

    drag->setPixmap(pixmap);
    drag->setHotSpot(QPoint(15, 20));

    std::cout << drag->exec&#40;Qt::MoveAction&#41; << std::endl;
    setCursor(Qt::OpenHandCursor);
    

    }
    @

    Which is almost copy paste from the various tutorials I found. This however does not work. When dragging a point, I get a Qt::ForbiddenCursor and the result from drag->exec(Qt::MoveAction) always returns 0 which is the Ignore action I believe.

    So my guess is that either the points do not want to move (although I don't know why) or either the scene prevents the drag (although I don't know why either)

    Does anyone have an idea what I'm overlooking? I fear it might be something terribly stupid but right now I have not the slightest idea what is wrong,

    Thanks in advance.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi again,

      Is all of this actually needed? I found on the internet (for example http://www.qtcentre.org/threads/9342-QGraphicsItem-itemIsMovable) that the movable flag should do the trick. So i removed all handlers from my scene and so on. The Point class now looks like:
      @
      InterpolationPoint::InterpolationPoint(const MsVector& ref) : pos(ref), iD(i++), hpos(2,1) {
      setAcceptedMouseButtons(Qt::LeftButton);
      setCursor(Qt::OpenHandCursor);
      setFlag(QGraphicsItem::ItemIsMovable, true);
      }

      InterpolationPoint::~InterpolationPoint() {

      }

      QRectF InterpolationPoint::boundingRect() const
      {
      return QRectF(-5, -5, 10, 10);
      }

      void InterpolationPoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
      Q_UNUSED(option);
      Q_UNUSED(widget);
      painter->setPen(QPen(Qt::black, 1));
      painter->setBrush(Qt::SolidPattern);
      painter->drawEllipse(-2, -2, 4, 4);
      setPos(QPointFVec(pos));
      }
      @

      Moving the itemismovable into the paint didn't fix the problem for me, I can not move the point across the scene which is basically all I want: movable points, they do not need to drop a shadow or whatever, just movable. How can I achieve this?

      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