title: Qt AbstractItemModel rearranging images
body: <p>I want to rearrange a set of images in a qlistview. Ive looked at the examples and I just can't get this to work. When I drag an image over another Image dropomimedata() is executed however its "data->hasImage()" is always false. When I drop an Image in empty space for some reason dropmimedata() isn't triggered at all. </p>
  
  <p>My model should look like this: </p>
  
  <p><a href="https://i.stack.imgur.com/nZcLz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nZcLz.png" alt="enter image description here"></a></p>
  
  <p>However after dragging into empty spaces it looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/QGEK8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QGEK8.png" alt="enter image description here"></a></p>
  
  <p>And when I drag an image over another nothing changes because hasImage is always false. What am I doing wrong? What am I missing?</p>
  
  <pre><code>#include "spritemodel.h"
  
  #include &lt;QDebug&gt;
  #include &lt;QMimeData&gt;
  
  SpriteModel::SpriteModel() : QAbstractListModel()
  {
  }
  
  void SpriteModel::setContents(QList&lt;QPair&lt;QImage, QOpenGLTexture*&gt;&gt; &amp;newList)
  {
      beginInsertRows(QModelIndex(), 0, newList.size());
      imageList = newList;
      endInsertRows();
  }
  
  int SpriteModel::rowCount(const QModelIndex &amp; parent) const
  {
      Q_UNUSED(parent);
      return imageList.size();
  }
  
  QVariant SpriteModel::data(const QModelIndex &amp; index, int role) const
  {
      if (role == Qt::DecorationRole)
          return imageList[index.row()].first;
      else if (role == Qt::DisplayRole)
          return "";
      else
          return QVariant();
  }
  
  Qt::DropActions SpriteModel::supportedDropActions() const
  {
      return Qt::CopyAction | Qt::MoveAction;
  }
  
  Qt::ItemFlags SpriteModel::flags(const QModelIndex &amp;index) const
  {
      Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
  
      if (index.isValid())
          return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
      else
          return Qt::ItemIsDropEnabled | defaultFlags;
  }
  
  bool SpriteModel::removeRows(int position, int rows, const QModelIndex &amp;parent)
  {
      beginRemoveRows(QModelIndex(), position, position+rows-1);
  
      for (int row = 0; row &lt; rows; ++row) {
          imageList.removeAt(position);
      }
  
      endRemoveRows();
      return true;
  }
  
  bool SpriteModel::insertRows(int position, int rows, const QModelIndex &amp;parent)
  {
      beginInsertRows(QModelIndex(), position, position+rows-1);
  
      QImage img(imageList[0].first.width(), imageList[0].first.height(), imageList[0].first.format());
      QOpenGLTexture *texture = new QOpenGLTexture(img);
  
      for (int row = 0; row &lt; rows; ++row) {
          imageList.insert(position, qMakePair(img, texture));
      }
  
      endInsertRows();
      return true;
  }
  
  bool SpriteModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role)
  {
      QImage img = value.value&lt;QImage&gt;();
      QOpenGLTexture *texture = new QOpenGLTexture(img);
  
      if (index.isValid() &amp;&amp; role == Qt::EditRole) {
  
          imageList.replace(index.row(), qMakePair(img, texture));
          emit dataChanged(index, index);
          return true;
      }
      return false;
  }
  
  QMimeData *SpriteModel::mimeData(const QModelIndexList &amp;indexes) const
  {
      QMimeData *mimeData = new QMimeData();
      QByteArray encodedData;
  
      QDataStream stream(&amp;encodedData, QIODevice::WriteOnly);
  
      foreach (const QModelIndex &amp;index, indexes) {
          if (index.isValid()) {
              QVariant img = data(index, Qt::DecorationRole);
              stream &lt;&lt; img;
          }
      }
  
      mimeData-&gt;setData("image/png", encodedData);
      return mimeData;
  }
  
  bool SpriteModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &amp;parent)
  {
      if (data-&gt;hasImage()) {
          qDebug() &lt;&lt; "wut";
          QImage img = qvariant_cast&lt;QImage&gt;(data-&gt;imageData());
          QOpenGLTexture *texture = new QOpenGLTexture(img);
  
          beginInsertRows(parent, 0, 1); // test
          imageList.insert(row, qMakePair(img, texture));
          endInsertRows();
  
          emit dataChanged(QModelIndex(),QModelIndex());
          return true;
      }
      return false;
  }
  </code></pre>