diff --git a/kcm/devicessortproxymodel.cpp b/kcm/devicessortproxymodel.cpp index b83e78ccf..02d02a616 100644 --- a/kcm/devicessortproxymodel.cpp +++ b/kcm/devicessortproxymodel.cpp @@ -40,10 +40,23 @@ void DevicesSortProxyModel::sourceDataChanged(QModelIndex , QModelIndex ) bool DevicesSortProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const { - QVariant leftData = sourceModel()->data(left, Qt::InitialSortOrderRole); - QVariant rightData = sourceModel()->data(right, Qt::InitialSortOrderRole); + QAbstractItemModel* model = sourceModel(); + Q_ASSERT(qobject_cast(model)); + + //Show connected devices first + int statusLeft = model->data(left, DevicesModel::StatusModelRole).toInt(); + int statusRight = model->data(right, DevicesModel::StatusModelRole).toInt(); + + if (statusLeft != statusRight) { + return statusLeft > statusRight; + } + + //Fallback to alphabetical order + QString nameLeft = model->data(left, DevicesModel::NameModelRole).toString(); + QString nameRight = model->data(right, DevicesModel::NameModelRole).toString(); + + return nameLeft > nameRight; - return leftData.toInt() > rightData.toInt(); } bool DevicesSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const