简体   繁体   中英

How to reorder/move items in QSortFilterProxyModel?

I am in the process of writing a Media Player application using Qt 5.8/QML with a C++ back end, and I am creating a queue of the next several songs. In order for the user to reorder the songs in the list, I need to move the items within the QSortFilterProxyModel that is filtering them. Is this possible without editing the sourceModel ? Additionally, the movement should trigger the move transition on the qml listview to animate the movement of the items. Any help would be greatly appreciated.

Use the QAbstractItemModel::moveRow method and the QAbstractItemModel::beginMoveRows and endMoveRows methods.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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