On this page

Sorting and Filtering with SortFilterProxyModel

Demonstrates how to sort and filter a C++ table model in QML with SortFilterProxyModel.

Screenshot of the application showing a process table with a search field, a state selector, a CPU range slider and a group of conditions above it

This example shows a list of processes, provided by a C++ table model, in a TableView. A QML SortFilterProxyModel sits between the model and the view. The user can search the processes by name, show only the processes in a given state, limit the CPU usage to a range, combine conditions so that a process must match all of them or any of them, and sort the table by clicking a column header.

The source model

ProcessModel is a QAbstractTableModel with a fixed list of processes. It is exposed to QML with QML_ELEMENT. Every column provides the pid, name, uid, state, cpuUsage and memory roles, so filters and sorters can use any of them. The value of the column itself is provided through Qt::DisplayRole.

enum Roles {
    PidRole = Qt::UserRole + 1,
    NameRole,
    UidRole,
    StateRole,
    CpuUsageRole,
    MemoryRole
};
Q_ENUM(Roles)

roleNames() maps each role to the name that is used in QML, for instance in the roleName property of filters and sorters:

QHash<int, QByteArray> ProcessModel::roleNames() const
{
    QHash<int, QByteArray> roles = QAbstractTableModel::roleNames();
    roles[PidRole] = "pid";
    roles[NameRole] = "name";
    roles[UidRole] = "uid";
    roles[StateRole] = "state";
    roles[CpuUsageRole] = "cpuUsage";
    roles[MemoryRole] = "memory";
    return roles;
}

The main window creates the model and passes it to ProcessTable:

ProcessModel {
    id: processModel
}

ProcessTable {
    id: processTable
    anchors.fill: parent
    anchors.margins: 8
    sourceModel: processModel
}

Filtering the rows

ProcessTable sets the model as the sourceModel of a SortFilterProxyModel, and uses the proxy model as the model of its TableView. A row is shown only when all enabled filters in the filters list accept it.

A RegExpFilter matches the name role against the text of the search field. The text is escaped, so that it is matched literally and any character can be typed:

RegExpFilter {
    roleName: "name"
    regExp: new RegExp(searchField.text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i")
},

A ValueFilter shows the processes in the state selected in a ComboBox. The filter is disabled when All states is selected:

ValueFilter {
    enabled: stateBox.currentIndex > 0
    roleName: "state"
    value: stateBox.currentText
},

A RangeFilter keeps the CPU usage between the two values of a RangeSlider. The proxy model updates while the slider is dragged:

RangeFilter {
    roleName: "cpuUsage"
    minimum: cpuRange.first.value
    maximum: cpuRange.second.value
},

Combining filters

The filters in the filters list must all accept a row for it to be shown. Filters can also be grouped with AllOfFilter and AnyOfFilter. An AllOfFilter accepts a row when all of its enabled child filters accept it, and an AnyOfFilter accepts a row when at least one of them does. Child filters that are not enabled are ignored, and a group with no enabled child filters accepts every row.

The Conditions group box lets the user pick three conditions: the process is running, the process uses more than 50 percent of the CPU, and the process uses more than 500 MB of memory. Two radio buttons select whether a process must match all of the selected conditions or any of them.

Each condition is a filter that is enabled by its check box. The conditions appear in both groups, and each group is enabled by its radio button, so only one of the groups is active at a time:

AllOfFilter {
    enabled: matchAll.checked
    ValueFilter {
        enabled: runningFilterToggle.checked
        roleName: "state"
        value: "Running"
    }
    RangeFilter {
        enabled: cpuFilterToggle.checked
        roleName: "cpuUsage"
        minimum: exclusive(50)
    }
    RangeFilter {
        enabled: memoryFilterToggle.checked
        roleName: "memory"
        minimum: exclusive(500)
    }
},
AnyOfFilter {
    enabled: matchAny.checked
    ValueFilter {
        enabled: runningFilterToggle.checked
        roleName: "state"
        value: "Running"
    }
    RangeFilter {
        enabled: cpuFilterToggle.checked
        roleName: "cpuUsage"
        minimum: exclusive(50)
    }
    RangeFilter {
        enabled: memoryFilterToggle.checked
        roleName: "memory"
        minimum: exclusive(500)
    }
}

For example, with Busy and High memory selected, Match all conditions shows the processes that use a lot of both CPU and memory, while Match any condition shows the processes that use a lot of either.

Sorting the rows

The proxy model has two sorters. The RoleSorter sorts by the role of the column that the user clicked. It compares the values of the role directly, which suits the numeric columns. The StringSorter sorts by name and ignores case, so that QtWebEngineProcess is sorted between qtdiag and sshd. The priority property sets the order in which the sorters are applied:

sorters: [
    RoleSorter {
        id: columnSorter
        enabled: root.sortColumn !== root.nameColumn
        roleName: root.columnRoles[root.sortColumn]
        sortOrder: Qt.DescendingOrder
        priority: 0
    },
    StringSorter {
        id: nameSorter
        roleName: "name"
        caseSensitivity: Qt.CaseInsensitive
        priority: 1
    }
]

When the user sorts by name, the RoleSorter is disabled and the StringSorter alone sets the order. For the other columns, the StringSorter sorts rows with the same value by name. Clicking a column header calls sortBy(), which sets the sortOrder of both sorters. Clicking the same header again reverses the order:

function sortBy(column: int) {
    if (column === root.sortColumn) {
        root.sortOrder = root.sortOrder === Qt.AscendingOrder
                ? Qt.DescendingOrder : Qt.AscendingOrder
    } else {
        root.sortColumn = column
        root.sortOrder = Qt.AscendingOrder
    }
    columnSorter.sortOrder = root.sortOrder
    nameSorter.sortOrder = column === root.nameColumn ? root.sortOrder : Qt.AscendingOrder
}

Running the Example

You can run the example from:

Example project @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.