Support numeric aware sorting on Windows and macOS

* Fix #8356 - Qt does not enable numeric aware sorting when using locale sort. Extracted both Windows and macOS locale aware sorting code and added the appropriate numeric aware flag.

Note: There is no std library way to do this so Linux is out of luck for now.
This commit is contained in:
Jonathan White 2022-08-13 17:19:05 -04:00
parent b0a68ea0de
commit 4c1e5ec74c
2 changed files with 15 additions and 0 deletions

View File

@ -16,6 +16,7 @@
*/ */
#include "SortFilterHideProxyModel.h" #include "SortFilterHideProxyModel.h"
#include <QCollator>
SortFilterHideProxyModel::SortFilterHideProxyModel(QObject* parent) SortFilterHideProxyModel::SortFilterHideProxyModel(QObject* parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
@ -41,3 +42,16 @@ bool SortFilterHideProxyModel::filterAcceptsColumn(int sourceColumn, const QMode
return sourceColumn >= m_hiddenColumns.size() || !m_hiddenColumns.at(sourceColumn); return sourceColumn >= m_hiddenColumns.size() || !m_hiddenColumns.at(sourceColumn);
} }
bool SortFilterHideProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
auto leftData = sourceModel()->data(left, sortRole());
auto rightData = sourceModel()->data(right, sortRole());
if (leftData.type() == QVariant::String) {
QCollator sorter;
sorter.setNumericMode(true);
return sorter.compare(leftData.toString(), rightData.toString()) < 0;
}
return QSortFilterProxyModel::lessThan(left, right);
}

View File

@ -32,6 +32,7 @@ public:
protected: protected:
bool filterAcceptsColumn(int sourceColumn, const QModelIndex& sourceParent) const override; bool filterAcceptsColumn(int sourceColumn, const QModelIndex& sourceParent) const override;
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override;
private: private:
QBitArray m_hiddenColumns; QBitArray m_hiddenColumns;