Can't get dragging and dropping to work



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



  • 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?


Log in to reply
 

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