Search code examples
qtqabstractitemmodelqlistview

QListView moveRow() from model not called


I'm having a Listview which should show a preview of an image. For this I created a the item "ListItem"

class ListItem
{
public:
    ListItem();
    ListItem(QString name, QImage image);
    QImage* previewIcon();
    void setPreviewIcon(QImage icon);
    QImage *image();
    void setImage(QImage* image);
    void setImage(QImage image);
    void setName(QString name);
    void setChecked(bool checked);
    QString name();
    bool checked();
private:
    QImage m_preview;
    QImage m_image;
    QString m_name;
    bool m_checked;
};

This model stores the image it self and a preview of it. This works fine for inserting and removing items:

class ListModel: public QAbstractListModel
{
    Q_OBJECT

public:
        ListModel(QObject *parent = nullptr);
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
        Qt::ItemFlags flags(const QModelIndex &index) const override;
        QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
        bool removeRow(int row, const QModelIndex &parent = QModelIndex());
        bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
        bool insertRow(int row, const QModelIndex &parent = QModelIndex());
        bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
        bool appendItem(ListItem* item);
        bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
        bool moveRow(const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild);
        Qt::DropActions supportedDropActions() const override;
        QHash<int, QByteArray> roleNames() const override;
private:
        ListItem* getItem(const QModelIndex &index) const;
 private:
    QList<ListItem*> m_scannedDocuments;
};

This is the setup of the QListView:

m_scannedDocumentsModel = new ListModel();
m_scannedDocuments = new QListView();
m_scannedDocuments->setModel(m_scannedDocumentsModel);
m_scannedDocuments->setDragDropMode(QAbstractItemView::InternalMove);
m_scannedDocuments->setMovement(QListView::Snap);
m_scannedDocuments->setDefaultDropAction(Qt::MoveAction);

Dragging and droping is fine for the preview, the name and if it is checked or not. The problem is the image "m_image". When I'm doing a move in the view, the view calls insertRows() and inserts a new item and removes the old item, but does not call moveRows.

Why moveRows() is not called?

Here you can find the full implementation: ListModel.cpp ListModel.h

Another approach would be to create a userRole for the image it self. Here I tried to reimplement roleNames() as

QHash<int, QByteArray> ListModel::roleNames() const {
    QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
    roles[Qt::UserRole] = "Qt::UserRole";
    return roles;
}

and check the roles in setdata()/data() but this didn't work either.

What is the best way to have a complex model and moving them in a listview?


Solution

  • As already answered in brief on the Qt interest list:

    Because moveRows was added in Qt 5.x, and drag and drop in Qt 4, and still uses insert+remove for backwards compatibility.

    I'd add that "moveRows" is I guess considered an "optimization" and not really a requirement for a model, and the view has no way to know if the move methods are really implemented. Since an editable model requires remove/insert functions, it's "safer" to call those by default.

    You can re-implement the model's dropMimeData() method and call moveRows() yourself. But, the caveat is you should return false from the method if you do this. If you return true to a Qt::MoveAction, the view will still try to remove the row from the old position in the model (which is obviously not what you want).