Important: Please read the Qt Code of Conduct -

Ending QDrag prematurely.

  • I want my application to accept the drag as a drop immediately as soon as the drag has entered. Is this possible?

    I have tried sending a QDropEvent to my widget on its dragEnterEvent() method, but the QDrag::exec() does not return anything (it doesn't count as a drop from the QDrag at all, I still see my pixmap and modified cursor).

  • Did you use QGraphicsItem?

  • No, I am using a QTabBar (I'm adding detachable tab functionality).

    If I find no solutions, I'm going to add a new thread with an event loop that simulates drag events.

  • Have you tried posting a mouseReleaseEvent upon entering the widget? Or would that undermine your plans?

  • I tried and it didn't work. I tried with both sending the event to the widget and to the application's event processor with no avail.
    QMouseEvent mouseEvent(QEvent::MouseButtonRelease, event->pos(), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
    QCoreApplication::postEvent(static_cast<QObject*>(this), &mouseEvent, Qt::NormalEventPriority);@

    Anyone have any more suggestions?

  • What code are you using to implement the drop? Nothing should stop you from you realizing it when the mouse enters (instead of later on). Does it work that far and the only step needed is to exit the drag-process?

  • It does realize when the mouse enters. The problem is forcing it to stop.

    @QDrag drag = new QDrag(this);
    mimeData = new QMimeData();
    =mimeData->setData("tab-detach", QByteArray::number(clickIndex));

    switch(drag->exec(Qt::MoveAction | Qt::CopyAction))
    case Qt::MoveAction:
    qDebug() << "Accepted";
    qDebug() << "Ignored";
    tabWidget->insertTab(position, dragWidget, dragText);

    void ResourceTabBar::dragEnterEvent(QDragEnterEvent *event)
    QByteArray a = event->mimeData()->data("tab-detach");
    if (!a.isEmpty() && dragTab != nullptr)
    qDebug() << dragTab;
    // This is where I've been trying to stop it.

    I can see the drag entered. I tried posting the code I posted previously to the comment in the latter code. I can make a dropEvent execute, but the drag->exec() does not end if I do.

  • What you are doing in case of something other than Qt::MoveAction is just reverting changes done before, I suppose?

    Are you actually implementing the dropEvent() to have full control over it?

    What happens if you call the base classes dropEvent() within the dragEnterEvent()? Something like the following:
    @void QWidget::dragEnterEvent(QDragEnterEvent *event)
    QWidget::dropEvent(event);//no calling of the "proper" base class implementation

    If nothing works, I would try to just call "delete drag" once it is not needed anymore (reparent it first). I don't know if that causes a crash, or if the drag finishes in a clean manner.

  • I handle the ignore, if necessary. The dropEvent simply accepts.

    The code you provided did not do anything. Your idea does not work either. I tried to delete the drag on the qDrag::targetChanged signal and it does nothing at all.

    I have tried checking the repository for the source and I cannot figure out how dragging works internally. What I do know is that on Windows (which is what platform I'm currently on), any events that I pass to QCoreApplication will be blocked until the QDrag exits. This is why I can't just post a drop event.

  • Have you tried calling QCoreApplication::processEvents() after posting the event?

    The code I provided was just an example, you would have to make it fit to your class. Basically, you would have to replace the "QWidget" by the name of your class.

  • I'm facing the same issue.tried every suggestion mentioned in the previous comments.Anyone have any more suggestions?

Log in to reply