Nếu beginMoveRows được sử dụng không hợp lý, nó có thể dẫn tới lỗi crash với coredump như sau:
Thread 1 (LWP 4486):
#0 0x00007fa23388663e in ?? () from /usr/lib/dri/i965_dri.so
No symbol table info available.
#1 0x00007fa2331bad1b in ?? () from /usr/lib/dri/i965_dri.so
No symbol table info available.
#2 0x00007fa243c92ab7 in QSGBatchRenderer::Renderer::renderMergedBatch(QSGBatchRenderer::Batch const*) () from /usr/lib/libQt5Quick.so.5
No symbol table info available.
#3 0x00007fa243c9339d in QSGBatchRenderer::Renderer::renderBatches() () from /usr/lib/libQt5Quick.so.5
No symbol table info available.
#4 0x00007fa243c97a3a in QSGBatchRenderer::Renderer::render() () from /usr/lib/libQt5Quick.so.5
No symbol table info available.
#5 0x00007fa243ca48a4 in QSGRenderer::renderScene(QSGBindable const&) () from /usr/lib/libQt5Quick.so.5
No symbol table info available.
m_list chính là một list con trỏ, ta đã khai báo, hay nói cách khác chính là triển khai phần ruột của lớp ảo QAbstractListModel này, chúng ta định nghĩa một public_slots f_move(...) để gọi được trên mã QML.
Chúng ta xem xét đoạn code dưới đây:
bool MyAbstractListModel::f_move(int _cIdx, int _tIdx, int _cnt)
{
if (_cIdx < 0
|| _cIdx >= rowCount()
|| _tIdx < 0
|| _tIdx >= rowCount() ){
return false;
}
if (_cIdx == _tIdx)
return true;
int from = _cIdx > _tIdx ? _cIdx : _tIdx;
int to = _cIdx < _tIdx ? _cIdx : _tIdx;
beginMoveRows(QModelIndex(),from, from, QModelIndex(), to);
qDebug() << "MyAbstractListModel::f_move ================================> " << from << " : " << to;
m_list.swap(from, to);
endMoveRows();
return true;
}
Điều đáng lưu ý là: beginMoveRows và endMoveRows,
Giá trị đầu vào: sourceFirst và sourceLast phải thỏa mãn logic là luôn nhỏ hơn destinationChild
Chúng ta cần cover lại giá trị đầu vào bởi from và to:
int from = _cIdx > _tIdx ? _cIdx : _tIdx;
int to = _cIdx < _tIdx ? _cIdx : _tIdx;
beginMoveRows(QModelIndex(),from, from, QModelIndex(), to);
qDebug() << "MyAbstractListModel::f_move ================================> " << from << " : " << to;
m_list.swap(from, to);
endMoveRows();