Qt drag / drop: cannot move when copy is enabled (Ubuntu Gnome)

I implement a view and a model where I want to support both moving elements inside (by dragging and dropping) and copying elements (by pressing Ctrl while dragging). I have done everything I need in accordance with the instructions. I installed the mime functions, I implemented removeRows () and flags (). The problem is when I drag, the copy operation is used by default (I get the cursor with a plus sign and it really copies the item, creating a new one in the model).

The only difference I see is this: if I only return Qt :: MoveAction to supportedDropActions (), it will only move. If I return (Qt :: CopyAction | Qt :: MoveAction), it will only be copied.

Any ideas? I want it to work as files in Nautilus (Gnome) or in Windows Explorer: drag and drop moves the icons around, ctrl + drag copies them.

+5
source share
3 answers

Sorry, I did not answer when I answered this question when I discovered what I was doing wrong. The code that confused me was in QAbstractItemView::startDrag():

if (d->defaultDropAction != Qt::IgnoreAction && (supportedActions & d->defaultDropAction))
        defaultDropAction = d->defaultDropAction;
    else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove)
        defaultDropAction = Qt::CopyAction;

The problem is that I did not set the defaultDropAction property in the widget, as in setDefaultDropAction( Qt::MoveAction );Therefore, startDrag () used CopyAction by default. If the defaultDropAction is Qt :: MoveAction, you can use the Ctrl keyboard to switch the drag and drop to the copy action, which is my desired behavior.

Qt, , . .

: , defaultDropAction - Qt:: MoveAction.

+1

, keyPressEvent , ctrl .   

keyPressEvent(QKeyEvent *e)
    {
        if(e->key() == Qt::Key_Control)
            bControlKeyPressed = true;
    }
    keyReleaseEvent (QKeyEvent *e)
    {
       if(e->key() == Qt::Key_Control)
          bControlKeyPressed = false;
    }

mouse pressEvent , Ctrl

`mousePressEvent()
{ 
  if (bControlKeyPressed) 
    //enable Qt::CopyAction 
else 
    //enable  Qt::MoveAction 
}` 
0

, :

. drap & drop ( , Qt, ), , , , "". , , .

If there is no way to determine the desired action from the Qt data, check the state of the Ctrl key and save the logical expression, copy or move it. Now that the Qt signals have been removed, check the action you selected and delete the moved row yourself.

(I personally use gtkmm, where copy-if-Ctrl-is-press works pefectly)

0
source

All Articles