Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sorting a QAbstractItemModelReplica via QSortFilterProxyModel doesn't update the view



  • Hi

    I use a QAbstractItemModelReplica (QAIMR) to display my data in a qml ListView in the client. I want to sort this data with a QSortFilterProxyModel (QSFPM).

    As long as I use the QSFPM in the client everything works as intended. Calling one of the following functions automatically triggers the sorting.

    // m_proxyModel is the QSortFilterProxyModel on the client
    
    void Client::setSortOrder(Qt::SortOrder sortOrder)
    {   
        m_proxyModel->sort(0, sortOrder);  
    }
    
    void Client::setSortType(int sortType)
    {
        m_proxyModel->setSortRole(Qt::UserRole + sortType);
    }
    

    Changing the sortOrder switches between ascending and descending and changing the sortType sets the role in the QSFPM according to which you want to sort

    But sorting on the client is much too slow, because with every call of the data() function the data must be sent from the server to the client. Therefore I tried to use the QSFPM on the server. But this led to the problem that the order is not updated in the client. I already checked that the data() function is called during sorting, but the order in the ListView always remains the same.

    // m_proxyModel is the QSortFilterProxyModel on the server
    // m_myRo is the remote object used to send the sortType and sortOrder from the client to the server
    
    connect(&m_myRo, &MyRemoteObject::sortTypeChanged, [this](int sortType) {     
        m_proxyModel.setSortRole(Qt::UserRole + sortType);        
    });
    
    connect(&m_myRo, &MyRemoteObject::sortOrderChanged, [this](Qt::SortOrder sortOrder) {      
        m_proxyModel.sort(0, sortOrder);
    });
    

    Finally I solved the problem by manually calling the dataChanged() signal. Additionally i had to call the sort function manually when setting the sortType.

    connect(&m_myRo, &MyRemoteObject::sortTypeChanged, [this](int sortType) {
        m_proxyModel.setSortRole(Qt::UserRole + sortType);
        m_proxyModel.sort(0, m_proxyModel.sortOrder());
        emit m_proxyModel.dataChanged(m_proxyModel.index(0, 0), m_proxyModel.index(m_proxyModel.rowCount() - 1, 0));
    });
    
    connect(&m_myRo, &MyRemoteObject::sortOrderChanged, [this](Qt::SortOrder sortOrder) {
        m_proxyModel.sort(0, sortOrder);
        emit m_proxyModel.dataChanged(m_proxyModel.index(0, 0), m_proxyModel.index(m_proxyModel.rowCount() - 1, 0));
    });
    

    It seems very unusual that I have to call some functions manually in the server case, but in the client case they work automatically. Therefore I would like to know if I am doing something wrong here, or if this is a bug, or if everything is working as intended.

    Best regards
    Suli


Log in to reply