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. Checking if a drag and drop operation is active?
QtWS25 Last Chance

Checking if a drag and drop operation is active?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.0k 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.
  • T Offline
    T Offline
    Thuan_Firelight
    wrote on 6 Apr 2020, 00:31 last edited by
    #1

    Hi all,

    I am noticing some undesirable behavior (from our point of view), that while a QDrag object is executing, keyboard shortcut can still get processed. This mean the owner of the QDrag could potentially get deleted while the drag is active. In Qt 5.7, I see they have introduced a static cancel function on QDrag but from the description it doesn't seem to work on macOS yet.

    We would like a consistent behavior and I was wondering if there is a way to tell if a QDrag is executing at the moment?

    I can see there is a QDragManager which is not public and trying to use it seems complicated (it includes "private/qobject_p.h"). Also tried the findChild() approach, but might have to consider iterating through QGraphicsScene on my own, the recursion in findChild() doesn't seem to do that.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 6 Apr 2020, 19:04 last edited by
      #2

      Hi,

      What would you like to do with that information ?
      One possible way would be to emit a signal from the object at the origin of the drag before starting the drag and one after it's done.

      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
      1
      • T Offline
        T Offline
        Thuan_Firelight
        wrote on 7 Apr 2020, 00:29 last edited by
        #3

        Hi SGaist,

        I have an application wide event filter and I was hoping if a QDrag object is active (i.e. we are in the process of drag and drop), we ignore all shortcut except escape key.

        R 1 Reply Last reply 7 Apr 2020, 11:00
        0
        • T Thuan_Firelight
          7 Apr 2020, 00:29

          Hi SGaist,

          I have an application wide event filter and I was hoping if a QDrag object is active (i.e. we are in the process of drag and drop), we ignore all shortcut except escape key.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 7 Apr 2020, 11:00 last edited by
          #4

          @Thuan_Firelight
          why do you handle shortcuts with an application wide eventfilter?
          Such a design is rather the source of your unexpected misbehavior.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          T 1 Reply Last reply 8 Apr 2020, 00:16
          1
          • R raven-worx
            7 Apr 2020, 11:00

            @Thuan_Firelight
            why do you handle shortcuts with an application wide eventfilter?
            Such a design is rather the source of your unexpected misbehavior.

            T Offline
            T Offline
            Thuan_Firelight
            wrote on 8 Apr 2020, 00:16 last edited by
            #5

            @raven-worx said in Checking if a drag and drop operation is active?:

            @Thuan_Firelight
            why do you handle shortcuts with an application wide eventfilter?
            Such a design is rather the source of your unexpected misbehavior.

            Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo. Do welcome any other suggestion that would allow for the same behavior that is not via a eventfilter. I can probably also do it in the bit where we push or pop from the undo stack, but that still requires knowledge that we are in a drag and drop operation.

            J R 2 Replies Last reply 8 Apr 2020, 04:55
            0
            • T Thuan_Firelight
              8 Apr 2020, 00:16

              @raven-worx said in Checking if a drag and drop operation is active?:

              @Thuan_Firelight
              why do you handle shortcuts with an application wide eventfilter?
              Such a design is rather the source of your unexpected misbehavior.

              Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo. Do welcome any other suggestion that would allow for the same behavior that is not via a eventfilter. I can probably also do it in the bit where we push or pop from the undo stack, but that still requires knowledge that we are in a drag and drop operation.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 8 Apr 2020, 04:55 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • T Thuan_Firelight
                8 Apr 2020, 00:16

                @raven-worx said in Checking if a drag and drop operation is active?:

                @Thuan_Firelight
                why do you handle shortcuts with an application wide eventfilter?
                Such a design is rather the source of your unexpected misbehavior.

                Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo. Do welcome any other suggestion that would allow for the same behavior that is not via a eventfilter. I can probably also do it in the bit where we push or pop from the undo stack, but that still requires knowledge that we are in a drag and drop operation.

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 8 Apr 2020, 07:41 last edited by raven-worx 4 Aug 2020, 07:42
                #7

                @Thuan_Firelight said in Checking if a drag and drop operation is active?:

                Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo

                Qt is a rich UI framework, thus it of course provides also a shortcut concept (using QShortcut)
                When using QShortcut it is all about setting the context properly. I am not aware of a system which triggers shortcuts during DnD operations anyway. QShortcut takes the current focus scope (and the set context) into account for determining which shortcut to trigger.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                T 1 Reply Last reply 9 Apr 2020, 01:01
                2
                • R raven-worx
                  8 Apr 2020, 07:41

                  @Thuan_Firelight said in Checking if a drag and drop operation is active?:

                  Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo

                  Qt is a rich UI framework, thus it of course provides also a shortcut concept (using QShortcut)
                  When using QShortcut it is all about setting the context properly. I am not aware of a system which triggers shortcuts during DnD operations anyway. QShortcut takes the current focus scope (and the set context) into account for determining which shortcut to trigger.

                  T Offline
                  T Offline
                  Thuan_Firelight
                  wrote on 9 Apr 2020, 01:01 last edited by
                  #8

                  @raven-worx said in Checking if a drag and drop operation is active?:

                  @Thuan_Firelight said in Checking if a drag and drop operation is active?:

                  Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo

                  Qt is a rich UI framework, thus it of course provides also a shortcut concept (using QShortcut)
                  When using QShortcut it is all about setting the context properly. I am not aware of a system which triggers shortcuts during DnD operations anyway. QShortcut takes the current focus scope (and the set context) into account for determining which shortcut to trigger.

                  Not sure if I understand, do you mean Qt is not support to do that? I have a simplified version below demonstrating what I am seeing :

                  int Application::exec()
                  {
                      QMainWindow* testWindow = new QMainWindow;
                      testWindow->resize(500, 500);
                      QTreeWidget* tree = new QTreeWidget();
                      tree->setAcceptDrops(true);
                      tree->setDragEnabled(true);
                      tree->setDragDropMode(QAbstractItemView::InternalMove);
                      testWindow->setCentralWidget(tree);
                  
                      QTreeWidgetItem* dad = new QTreeWidgetItem(tree,0);
                      dad->setText(0, "Hello");
                      QTreeWidgetItem*  child = new QTreeWidgetItem(dad,0);
                      child->setText(0, "World");
                  
                      QMenu* testMenu = testWindow->menuBar()->addMenu("Test");
                      QAction* action = testMenu->addAction("Keyboard Shortcut Triggered");
                      action->setShortcut(Qt::ControlModifier + Qt::Key_D);
                      connect(action, &QAction::triggered, testWindow, [testWindow]() { qDebug() << "Action triggered!!"; });
                      testWindow->showNormal();
                      
                      return QApplication::exec();
                  }
                  

                  As I am dragging the item in the tree widget while holding the left mouse button, I can hit Ctrl+D to trigger the action and I am seeing the "Action triggered" getting printed to the console. It is not exactly clear cut though since the log only appears intermittently (i.e. if I hold down Ctrl and hit D 5 times, I might see the log only 2 times as I drag around inside the window).

                  According to the doc, the shortcut context for QAction is Qt::WindowShortcut which sounds about right to me.

                  R 1 Reply Last reply 9 Apr 2020, 11:05
                  0
                  • T Thuan_Firelight
                    9 Apr 2020, 01:01

                    @raven-worx said in Checking if a drag and drop operation is active?:

                    @Thuan_Firelight said in Checking if a drag and drop operation is active?:

                    Currently we don't, but I imagine handling at every widget level is going to be a nightmare if the behavior intented was that when dragging and dropping user shouldn't be allowed to perform keyboard shortcut such as undo or redo

                    Qt is a rich UI framework, thus it of course provides also a shortcut concept (using QShortcut)
                    When using QShortcut it is all about setting the context properly. I am not aware of a system which triggers shortcuts during DnD operations anyway. QShortcut takes the current focus scope (and the set context) into account for determining which shortcut to trigger.

                    Not sure if I understand, do you mean Qt is not support to do that? I have a simplified version below demonstrating what I am seeing :

                    int Application::exec()
                    {
                        QMainWindow* testWindow = new QMainWindow;
                        testWindow->resize(500, 500);
                        QTreeWidget* tree = new QTreeWidget();
                        tree->setAcceptDrops(true);
                        tree->setDragEnabled(true);
                        tree->setDragDropMode(QAbstractItemView::InternalMove);
                        testWindow->setCentralWidget(tree);
                    
                        QTreeWidgetItem* dad = new QTreeWidgetItem(tree,0);
                        dad->setText(0, "Hello");
                        QTreeWidgetItem*  child = new QTreeWidgetItem(dad,0);
                        child->setText(0, "World");
                    
                        QMenu* testMenu = testWindow->menuBar()->addMenu("Test");
                        QAction* action = testMenu->addAction("Keyboard Shortcut Triggered");
                        action->setShortcut(Qt::ControlModifier + Qt::Key_D);
                        connect(action, &QAction::triggered, testWindow, [testWindow]() { qDebug() << "Action triggered!!"; });
                        testWindow->showNormal();
                        
                        return QApplication::exec();
                    }
                    

                    As I am dragging the item in the tree widget while holding the left mouse button, I can hit Ctrl+D to trigger the action and I am seeing the "Action triggered" getting printed to the console. It is not exactly clear cut though since the log only appears intermittently (i.e. if I hold down Ctrl and hit D 5 times, I might see the log only 2 times as I drag around inside the window).

                    According to the doc, the shortcut context for QAction is Qt::WindowShortcut which sounds about right to me.

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 9 Apr 2020, 11:05 last edited by raven-worx 4 Sept 2020, 11:12
                    #9

                    @Thuan_Firelight said in Checking if a drag and drop operation is active?:

                    As I am dragging the item in the tree widget while holding the left mouse button, I can hit Ctrl+D to trigger the action and I am seeing the "Action triggered" getting printed to the console.

                    oh really?! i wasn't aware of that. Doesn't make sense to me though, sicne CTRL should be handled by the drag itself.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0

                    9/9

                    9 Apr 2020, 11:05

                    • Login

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