Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    How do I make items in qlistview (iconmode) align neatly?

    General and Desktop
    drag and drop qlistview
    1
    1
    1844
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      Fundies last edited by

      All I want is for my QImages in my model to align neatly after drag and drop. How do I make things align nicely after drop? Also when I drop one Image upon another it gets deleted. I want it to push the images over not delete. I've looked at the examples but I can't figure this out... Anyone know how to fix this?

      I want: good
      But I am getting:
      bad

      AnimationView::AnimationView(QWidget* parent) : QListView(parent)
      {
          setSelectionMode(QAbstractItemView::ExtendedSelection);
          setViewMode(QListView::IconMode);
          resize(800, 600);
          setDragEnabled(true);
          setDragDropMode(QAbstractItemView::InternalMove);
          setDropIndicatorShown(true);
          setDefaultDropAction(Qt::MoveAction);
          setAcceptDrops(true);
          setDragDropOverwriteMode(false);
      }
      
      #include "spritemodel.h"
      
      #include <QDebug>
      
      SpriteModel::SpriteModel() : QAbstractListModel()
      {
      }
      
      void SpriteModel::setContents(QList<QPair<QImage, QOpenGLTexture*>> &newList)
      {
          beginInsertRows(QModelIndex(), 0, newList.size());
          imageList = newList;
          endInsertRows();
      }
      
      int SpriteModel::rowCount(const QModelIndex & parent) const
      {
          Q_UNUSED(parent);
          return imageList.size();
      }
      
      QVariant SpriteModel::data(const QModelIndex & 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 &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 &parent)
      {
          beginRemoveRows(QModelIndex(), position, position+rows-1);
      
          for (int row = 0; row < rows; ++row) {
              imageList.removeAt(position);
          }
      
          endRemoveRows();
          return true;
      }
      
      bool SpriteModel::insertRows(int position, int rows, const QModelIndex &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 < rows; ++row) {
              imageList.insert(position, qMakePair(img, texture));
          }
      
          endInsertRows();
          return true;
      }
      
      bool SpriteModel::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          QImage img = value.value<QImage>();
          QOpenGLTexture *texture = new QOpenGLTexture(img);
      
          if (index.isValid() && role == Qt::EditRole) {
      
              imageList.replace(index.row(), qMakePair(img, texture));
              emit dataChanged(index, index);
              return true;
          }
          return false;
      }
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post