[Interest] QListView moveRow() from model not called

Martin Marmsoler martin.marmsoler at gmail.com
Sun Dec 1 23:07:32 CET 2019


I already postet the question here (
https://stackoverflow.com/questions/59130393/qlistview-moverow-from-model-not-called),
but maybe it is better to write here.

I'm having a Listview which should show a preview of an image. The model it
self contains also the original image. In the listview it should be able to
change the order of the items, where I have problems.

Here you can find the ListItem and the Listmodel:

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
<https://github.com/Murmele/skanlite/blob/ListViewInSkanlite/src/ListModel.cpp>
ListModel.h
<https://github.com/Murmele/skanlite/blob/ListViewInSkanlite/src/ListModel.h>

Another approach I tested is, 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 implemented the role Qt::UserRole in setdata()/data() but this doesn't
work either.

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


Cheers,

Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191201/930701ef/attachment.html>


More information about the Interest mailing list