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. Prevent QDragEnterEvent from being propagated to parent?

Prevent QDragEnterEvent from being propagated to parent?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 912 Views
  • 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.
  • O Offline
    O Offline
    olowo726
    wrote on last edited by
    #1

    Hi,

    I would like to prevent a QDragEnterEvent from being propagated to parent. In my widget I override QWidget::dragEnterEvent(QDragEnterEvent *event) to inspect the data and call event->accept() if the data can be dropped on the widget. How can I prevent the event from being propagated to the parent if I don't call event->accept()?

    Things I have tried without success

    • Override QObject::event(QEvent * e). I case of the event being a QEvent::DragEnter I first call dragEnterEvent and then return true. For other events I call QWidget:event(). QDragEnterEvent still gets propagated to parent widget.
    • Install an event filter. I case of the event being a QEvent::DragEnter I first call dragEnterEvent and then return true. For other events return false. QDragEnterEvent still gets propagated to parent widget.

    Any ideas?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What is the issue about letting the event propagate if you do not handle it in that widget ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What is the issue about letting the event propagate if you do not handle it in that widget ?

        O Offline
        O Offline
        olowo726
        wrote on last edited by
        #3

        @SGaist It's an MDI application. The user can create new MDI child windows by drag and drop items on empty space in the MDI parent. These MDI child windows are of different type handling different items. So with propagation what's happening is that the user drags an item of type X on a MDI child which only supports items of type Y. The MDI child window rejects the drag but since the event is propagated to the parent which approves it and to the user it looks like the MDI child accepts the drop. When the user does the drop the MDI parent instead creates a new child. Not what the user expects.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you share your current implementation of the drag and drop ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • O Offline
            O Offline
            olowo726
            wrote on last edited by
            #5

            More background info: the MDI child have a plot area and a tableview area. Drops on the plot area are forwarded to the model of the table view which implements the actual actions to tka.e

            void YtScopeSection::dragEnterEvent(QDragEnterEvent *event)
            {
                // The table view model will accept drops on itself but we cannot accept drops from the table view to our own plot
                // area. Hence check that first.
                if (event->mimeData()->hasFormat("application/x-NewlineSeparatedListOfMeasurementsAndCalibrations") &&
                    event->mimeData()->hasFormat("source"))
                {
                    QByteArray source = event->mimeData()->data("source");
                    QByteArray us(QString::number((quintptr)&model).toUtf8());
                    if (source == us)
                    {
                        // Cannot drop from our own table model to plot area => do not accept event.
                        event->ignore();
                    }
                    else
                    {
                        // Drag source isn't our own table view => delegate to model.
                        if (model.canDropMimeData(event->mimeData(), Qt::MoveAction, 0, 0, model.createIndexName(0)))
                        {
                            event->acceptProposedAction();
                        }
                        else
                        {
                            // Do not accept event => empty on purpose.
                            event->ignore();
                        }
                    }
                }
                else
                {
                    // Drag source isn't our own table view => delegate to model.
                    // The last four arguments are not used.
                    if (model.canDropMimeData(event->mimeData(), Qt::MoveAction, 0, 0, model.createIndexName(0)))
                    {
                        event->acceptProposedAction();
                    }
                    else
                    {
                        // Do not accept event => empty on purpose.
                        event->ignore();
                    }
                }
            }
            
            void YtScopeSection::dropEvent(QDropEvent *event)
            {
                // The last four arguments are not used.
                if (model.dropMimeData(event->mimeData(), Qt::MoveAction, 0, 0, model.createIndexName(model.rowCount())))
                {
                    event->acceptProposedAction();
                }
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              To avoid the issue of accidentally creating new mdi child, you should check whether there's already a widget under the cursor and in this case do nothing.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • O Offline
                O Offline
                olowo726
                wrote on last edited by
                #7

                How about setting the drop action of the event to ignore in the MDI child?

                Do you know why I fail to prevent the events from being propagated? I think what would have been the most elegant solution.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  An event that is ignored means that the current widget will not process it, thus it will go up the chain.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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