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

标签 c++ qt listview qsortfilterproxymodel

我正在使用带有C++后端的Qt 5.8 / QML编写Media Player应用程序,并且正在创建接下来几首歌曲的队列。为了让用户对列表中的歌曲重新排序,我需要在QSortFilterProxyModel中移动正在过滤它们的项目。不编辑sourceModel是否可以?此外,移动应触发move上的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();
    }
}

关于c++ - 如何在QSortFilterProxyModel中重新排序/移动项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668594/

相关文章:

c++ - 使用 extern "C"时 C++ 代码中的函数重载

c++ - 原子 128 位内存模式选择

java - 在 ListView 中添加 onClickListener

java - 如何在 ArrayAdapter 中为 ListView 使用多个 TextView ?

c++ - 通过引用传递动态变量的指针

C++输出数组中的最高元素

c++ - Qt:mouseMoveEvent并干扰子对象的hoverEnterEvent

c++ - 如何在 Qt Widgets 中删除带有插槽的按钮

Qt:如何更改橡皮筋选择颜色

listview - Win32 虚拟 ListView : Cannot select full row