繁体   English   中英

如何在QSortFilterProxyModel中重新排序/移动项目?

[英]How to reorder/move items in QSortFilterProxyModel?

我正在使用带有C ++后端的Qt 5.8 / QML编写Media Player应用程序,并且正在创建接下来几首歌曲的队列。 为了使用户对列表中的歌曲重新排序,我需要在QSortFilterProxyModel中移动正在过滤它们的项目。 不编辑sourceModel就可以吗? 此外,移动应触发qml listview上的移动过渡,以动画化项目的移动。 任何帮助将不胜感激。

使用QAbstractItemModel :: moveRow方法和QAbstractItemModel :: beginMoveRows和endMoveRows方法。

void MyProxyModel::moveUp(const int itemIndex) {
    if(itemIndex > 0 && itemIndex < rowCount())
    {
        beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), 
        itemIndex - 1);
        moveRow(QModelIndex(), itemIndex, QModelIndex(), itemIndex - 1);
        endMoveRows();
    }
}

void MyProxyModel::moveDown(const int itemIndex) {
    if(itemIndex >= 0 && itemIndex < rowCount() - 1) 
    {
        beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), 
        itemIndex + 2);
        moveRow(QModelIndex(), itemIndex, QModelIndex(), itemIndex + 2);
        endMoveRows();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM