Drag and drop items with link action is not working

  • Hi All,

    I have my own class which is inherited from QTreeView. I am unable to drag and drop items with link action. If understand well, with link action, we can drag the references of the items so that any changes made in the source items will reflect the destination items(which is my requirement).
    Unfortunately, I couldn’t succeed. Can someone help me?


  • Choosing the Qt::LinkAction (in the dragEnterEvent() of the target widget and at some point when the drag & drop operation is started) does not automatically link anything (unless everything is set up like this in the class you are using). You need to implement the dropEvent() for the target widget and take care of the linking yourself. Sharing an object/a pointer might be a solution, but maybe there is a more elegant one.

    Doesn't sound too difficult to me, but maybe you didn't give all the details.

  • Thanks four answer.

    I have my own class customTreeView which is inherited from QTreeView. In this view i have set the model (QStandardItemModel) which has some items. Now I want to drag these items from this view to other view (qtreeview). for this, pls find below functions which i have overwritten.

    void CustomTreeView::startDrag(Qt::DropActions supportedActions)
    QModelIndexList indexes = this->selectedIndexes();
    if (indexes.count() > 0)
    QMimeData *mimeData = model()->mimeData( indexes );
    QDrag *drag = new QDrag(this);
    drag->setMimeData( mimeData);
    Qt::DropAction dropAction = drag->exec(Qt::LinkAction);

    void CustomTreeView::dropEvent(QDropEvent *event)
    if(event->proposedAction() == Qt::CopyAction | Qt::MoveAction | Qt::LinkAction)
    const QMimeData *mimedata = event->mimeData();

    I want to link the data from this view to other, e.g if i drag any item and then if i rename it in this source view then that item should change in destination view also...

  • I have been using a customized QTableView in one of my programs. First step to make items drag&droppable is to reimplement

    • that's where the operation starts.

    Do something like this in there:
    @QByteArray itemData;
    //fill that byte array, probably you'll need a QDataStream
    QMimeData *mimeData = new QMimeData;
    mimeData->setData("application/x-dndentries", itemData);//the string can be chosen by you

    QDrag *drag = new QDrag(this);
    drag->exec(Qt::CopyAction | Qt::MoveAction | Qt::LinkAction, Qt::CopyAction);//second one defines the standard action@

    Afterwards you should reimplement the dragEnterEvent (if items can originate at a different ModelView), something simple might suffice:
    QTreeView::dragEnterEvent(event);//remember to call the base implementation!

    And, finally, the dropEvent. What you are doing in your dropEvent isn't achieving anything. You actually have to pull the dragged object back out of the QMimeData (probably using QDataStream again), and manually "link" it by reusing it's pointer, I guess. It won't just happen on its own!

    You already discovered that other thread where I posted, I gave a link to the drag and drop examples there. They are a really good read, take a look at them.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.