Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I make items in qlistview (iconmode) align neatly?

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

Scheduled Pinned Locked Moved General and Desktop
drag and dropqlistview
1 Posts 1 Posters 2.0k Views
  • 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 Offline
    F Offline
    Fundies
    wrote on 26 Jul 2015, 04:00 last edited by
    #1

    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
    0

    1/1

    26 Jul 2015, 04:00

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved