void ThingieListModel::move(int from, int to)
void ThingieListModel::move(int from, int to)
To copy to clipboard, switch view to plain text mode
, then everything works correctly. But in my case, this solution does not work, since in the current project, delegates are loaded asynchronously using Loader and when beginResetModel is called, the UI displays how they are all reloaded
/* Author: Remy van Elst, https://raymii.org
* License: GNU AGPLv3
*/
#include "ThingieListModel.h"
#include <QDebug>
ThingieListModel
::ThingieListModel(QObject *parent
) :{
}
void ThingieListModel::updateFromVector(std::vector<Thingie*> newThingies)
{
qDebug() << "updateFromVector";
beginResetModel();
_thingies.clear();
for (const auto &item : newThingies)
{
_thingies << item;
}
endResetModel();
}
QHash<int, QByteArray> ThingieListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[ColorRole] = "color";
roles[ModelIndexRole] = "modelIndex";
return roles;
}
{
if (!index.isValid())
{
}
const Thingie *thingie = _thingies[index.row()];
switch (role)
{
case NameRole:
return thingie->name();
case ColorRole:
return thingie->color();
case ModelIndexRole:
if (std::find(_thingies.begin(), _thingies.end(), thingie) != _thingies.end()) {
int d = std::distance(_thingies.begin(), std::find(_thingies.begin(), _thingies.end(), thingie));
qDebug() << d;
return d;
} else {
return -1;
}
default:
}
}
int ThingieListModel
::rowCount(const QModelIndex &) const {
return _thingies.count();
}
void ThingieListModel::move(int from, int to)
{
qDebug() << "move " << from << ";" << to;
if(from >= 0 && from < rowCount() && to >= 0 && to < rowCount() && from != to) {
if(from == to - 1) { // Allow item moving to the bottom
to = from++;
}
// beginResetModel();
qInfo() << "model move from: " << from << " to: " << to;
_thingies.move(from, to);
endMoveRows();
// endResetModel();
}
}
void ThingieListModel::createDublicate(int index)
{
int insertPosition = index + 1;
beginInsertRows
(QModelIndex(), insertPosition, insertPosition
);
Thingie *newThingie = new Thingie{_thingies[index]->name(), static_cast<int>(_thingies.size()), this};
newThingie->setColor(_thingies[index]->color());
_thingies.insert(insertPosition, newThingie);
endInsertRows();
}
{
qDebug() << "print";
for(int i = 0; i < _thingies.size(); ++i) {
tmp.append(": ");
tmp.append(_thingies.at(i)->name());
tmp.append("; ");
}
return tmp;
}
/* Author: Remy van Elst, https://raymii.org
* License: GNU AGPLv3
*/
#include "ThingieListModel.h"
#include <QDebug>
ThingieListModel::ThingieListModel(QObject *parent) :
QAbstractListModel(parent)
{
}
void ThingieListModel::updateFromVector(std::vector<Thingie*> newThingies)
{
qDebug() << "updateFromVector";
beginResetModel();
_thingies.clear();
for (const auto &item : newThingies)
{
_thingies << item;
}
endResetModel();
}
QHash<int, QByteArray> ThingieListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[ColorRole] = "color";
roles[ModelIndexRole] = "modelIndex";
return roles;
}
QVariant ThingieListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
const Thingie *thingie = _thingies[index.row()];
switch (role)
{
case NameRole:
return thingie->name();
case ColorRole:
return thingie->color();
case ModelIndexRole:
if (std::find(_thingies.begin(), _thingies.end(), thingie) != _thingies.end()) {
int d = std::distance(_thingies.begin(), std::find(_thingies.begin(), _thingies.end(), thingie));
qDebug() << d;
return d;
} else {
return -1;
}
default:
return QVariant();
}
}
int ThingieListModel::rowCount(const QModelIndex &) const
{
return _thingies.count();
}
void ThingieListModel::move(int from, int to)
{
qDebug() << "move " << from << ";" << to;
if(from >= 0 && from < rowCount() && to >= 0 && to < rowCount() && from != to) {
if(from == to - 1) { // Allow item moving to the bottom
to = from++;
}
// beginResetModel();
beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
qInfo() << "model move from: " << from << " to: " << to;
_thingies.move(from, to);
endMoveRows();
// endResetModel();
}
}
void ThingieListModel::createDublicate(int index)
{
int insertPosition = index + 1;
beginInsertRows(QModelIndex(), insertPosition, insertPosition);
Thingie *newThingie = new Thingie{_thingies[index]->name(), static_cast<int>(_thingies.size()), this};
newThingie->setColor(_thingies[index]->color());
_thingies.insert(insertPosition, newThingie);
endInsertRows();
}
QString ThingieListModel::print()
{
qDebug() << "print";
QString tmp;
for(int i = 0; i < _thingies.size(); ++i) {
tmp.append(QString::number(i));
tmp.append(": ");
tmp.append(_thingies.at(i)->name());
tmp.append("; ");
}
return tmp;
}
To copy to clipboard, switch view to plain text mode
Bookmarks