Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What enum Qt::DropAction really do?



  • Hi,

    I have an example project "Draggable Icons":

    https://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html
    

    An there is a line:

    if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
    

    And in function void DragWidget::dropEvent(QDropEvent *event) we set a value, which will be returned by drag->exec():

    • event->acceptProposedAction() -> drag->exec() will return Qt::CopyAction
    • event->setDropAction(Qt::MoveAction); event->accept(); -> drag->exec() will return Qt::MoveAction

    And this returned value will be important to know, what we should do: close widget or set a new Pixmap:

        if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
            child->close();
        } else {
            child->show();
            child->setPixmap(pixmap);
        }
    

    So for me the enum Qt::DropAction in this example do nothing. This is only a nice way to show a programmer, what he/she do. When he/she see a word "move" in his/her code, he/she know that here is a some move action. And it is imporant in if statement to know what do. But in docs I can see:

    Qt::CopyAction	0x1	Copy the data to the target.
    Qt::MoveAction	0x2	Move the data from the source to the target.
    

    Where is that data? Which data?

    If you don't understand me:
    I can change drag->exec() to:

    if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::LinkAction) == Qt::MoveAction) 
    

    and this project do the same things.

    If I change drag->exec() to:

    if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::TargetMoveAction) == Qt::MoveAction)
    

    This project do the same things.


  • Lifetime Qt Champion

    @qwe3 said in What enum Qt::DropAction really do?:

    Which data?

    The data being dragged I guess?



  • @jsulm Is that enum Qt::DropAction only to return in drag->exec() in a nice form? Not a number like 1,2,3, but Qt::DropAction::Move, Qt::DropAction::Copy etc ?

    Or in some function ( like event->setDropAction() ) really do something with dragged data?

    I don't see a difference:

    drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::LinkAction) 
    drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::TargetMoveAction) 
    drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) 
    

    For me Qt::CopyAction is only number, which is returned from drag->exec().

    For me in docs we can change:

    Qt::CopyAction	0x1	Copy the data to the target.
    Qt::MoveAction	0x2	Move the data from the source to the target.
    

    To:

    Qt::CopyAction	0x2	Move the data from the source to the target.
    Qt::MoveAction	0x1	Copy the data to the target.
    

    And it still be ok.


  • Lifetime Qt Champion

    @qwe3 It is explained in the documentation (https://doc.qt.io/qt-5/qdrag.html#exec-1):
    "The drop actions that the user can choose from are specified in supportedActions."
    Second parameter: "The defaultDropAction determines which action will be proposed when the user performs a drag without using modifier keys."



  • @jsulm Yes. And I set in defaultDropAction for example Qt::LinkAction. And run example app. Next I set as a defaultDropAction, Qt::TargetMoveAction ). And run example app. And I don't see a difference.

    And if I will be a programmer, which works in QT Company and I will add to enum Qt::DropAction for example Qt::abcdef and set in defaultDropAction that Qt::abcdef effect in this example app will be the same.

    So there is any difference in setting Qt::LinkAction and Qt::CopyAction? Or this is just a name?


  • Lifetime Qt Champion

    @qwe3 Second parameter: "The defaultDropAction determines which action will be proposed when the user performs a drag without using modifier keys.". So, the second parameter specifies what exec() will return if user does not use modifier key. Like in this example: https://doc.qt.io/qt-5/qtwidgets-draganddrop-fridgemagnets-example.html

    if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
        child->close();
    else
        child->show();
    

    In this example, if user does not use modifier key CopyAction will be returned by exec(), else MoveAction.



  • @jsulm Drag move events occur when the cursor enters a widget, when it moves within the widget, and when a modifier key is pressed on the keyboard while the widget has focus.

    I understand "Drag move events occur when the cursor enters a widget, when it moves within the widget", but I don't understand "and when a modifier key is pressed on the keyboard while the widget has focus". I don't use keyboard when run this fridge magnets app ( only mouse ).


  • Lifetime Qt Champion

    @qwe3 said in What enum Qt::DropAction really do?:

    I don't use keyboard when run this fridge magnets app ( only mouse )

    You don't have to use it, but you can...



  • @jsulm I know I have many questions, but I would like to really understand this drag and drop. So most important for me question:
    for example I have to create simple project when I have big widget and many labels on it. If I drag and drop any label I have to change only it position ( no copy ). So when I started with 10 labels, at the end of app run I will have still 10 labels. So I can use:

    if ((drag->exec(Qt::CopyAction, Qt::CopyAction ) == Qt::CopyAction )
    {
        // moveThatLabel
    }
    

    So I will use only Qt::CopyAction value in application, where I only move labels. Of course, after 1 year, when I will see my code I will be like "What? I see Qt::CopyAction, but I only move labels, so why I don't use Qt::MoveAction?". But using Qt::CopyAction in this example is correct on QT site?


  • Lifetime Qt Champion

    @qwe3 said in What enum Qt::DropAction really do?:

    I see Qt::CopyAction, but I only move labels, so why I don't use Qt::MoveAction?"

    Well, what you do in that if() {} block is up to you. If you move instead of copying then it is your issue, Qt has no control over your code.
    "But using Qt::CopyAction in this example is correct on QT site?" - sure, why not? Qt does not know what you are doing after exec().



  • @jsulm Yes and no. I check all possibilites and when I did:

        auto dragReturnedValue = drag->exec(Qt::ActionMask, Qt::ActionMask);
        qInfo()<<dragReturnedValue;
        if (dragReturnedValue == Qt::ActionMask) {
            child->show();
            child->setPixmap(pixmap);
        }
    

    The dragReturnedValue is not equal Qt::ActionMask. It is equal Qt::LinkAction, so "Qt::ActionMask" is not only "word". ( In my code I don't use event->setDropAction(Qt::LinkAction) ) I see difference in using Qt::ActionMask and Qt::CopyAction in defaultDropAction.

    The same with:

    dragReturnedValue = drag->exec(Qt::IgnoreAction, Qt::IgnoreAction)
    

    Here I can't drop labels ( in my code I don't use event->setDropAction(Qt::IgnoreAction) )

    EDIT: And there are other small icons when I started drag ( right arrow, plus, etc. )


  • Lifetime Qt Champion

    Hi,

    ActionMask as it names suggests is to be used as mask with for example Qt::TargetMoveAction to just check whether it's a move action and you'll do something else later on if it's a standard or target move action.

    The famous third parameter shall be one of the value you pass as possible action.

    It does not make sense to make the default action link if you only support move and copy.


Log in to reply