[SOLVED] Drag and Drop Issue



  • Hi,
    I have a model which I derived from QStandardItemModel that I want to DROP onto.

    I have it working the way I want when I drop it onto a valid parent. The four methods I over-rode to get that to work are below.

    My issue is in the dropMimeData() method. If I don't have a valid parent, I don't want the Action (MoveAction, in this case) to be performed. I AM returning false from this method, which is what I assumed I needed to do to indicate that the action could not be completed.

    Is this a Qt bug, or am I missing something?

    Thanks in advance for any help!!!

    @
    Qt::ItemFlags MapLayerItemModel::flags (const QModelIndex &index) const
    {
    Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
    if (index.isValid())
    {
    return Qt::ItemIsDropEnabled | defaultFlags; // Only allow dropping on valid items...
    }

    return defaultFlags;
    }

    Qt::DropActions MapLayerItemModel::supportedDropActions () const
    {
    return Qt::MoveAction | Qt::CopyAction;
    }

    QStringList MapLayerItemModel::mimeTypes () const
    {
    QStringList types = __super::mimeTypes();
    if (!types.contains("text/plain"))
    {
    types << "text/plain";
    }

    return types;
    }

    bool MapLayerItemModel::dropMimeData (const QMimeData* data, Qt::DropAction action, int row, int col, const QModelIndex &parentIndex)
    {
    if (parentIndex.isValid())
    {
    if (data->hasText())
    {
    if (addNewLayerFromFilePath(data->text(), parentIndex))
    {
    return true;
    }
    }
    }

    return false;
    }
    @



  • SOLVED: My bad: The issue was in the flags() method. The default flags I was getting from the base class QStandardItemModel had Qt::ItemIsDropEnabled set to true. So, in my case, I was ALWAYS returning true for this, even with invalid indexes. Once I fixed that, it all worked as I expected.


  • Moderators

    Glad you got it working! Thanks for the explanation of what the problem was. Hopefully that will help others who might have the same issue in the future.

    Please be sure and edit your original post to add [Solved] to the title.

    Thanks!



  • Done... Thanks for the reminder...


Log in to reply
 

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