Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u014746838/article/details/87620022
Brief
For the sorting section above, we have an extension of two points:
- The, the following data string preceding a data comparator according to the comparison string in the shaping.
- The plastic is displayed as a string, while still sorting it properly.
effect
Size display processing
// 定义GB、MB、KB的计算常量
const int GB = 1024 * 1024 * 1024;
const int MB = 1024 * 1024;
const int KB = 1024;
QString bytesToGBMBKB(qint64 size)
{
if (size / GB >= 1)
return QString("%1 GB").arg(QString::number(size / (float)GB, 'f', 2));
else if (size / MB >= 1)
return QString("%1 MB").arg(QString::number(size / (float)MB, 'f', 2));
else if (size / KB >= 1)
return QString("%1 KB").arg(QString::number(size / (float)KB, 'f', 2));
else
return QString("%1 Bytes").arg(size);
}
QAbstractTableModel
Here only the modified line of code, and the code is the same as the other sections.
// 表格项数据
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
int nRow = index.row();
int nColumn = index.column();
FileRecord record = m_recordList.at(nRow);
switch (role)
{
case Qt::TextColorRole:
return QColor(Qt::white);
case Qt::TextAlignmentRole:
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
case Qt::DisplayRole:
{
if (nColumn == FILE_NAME_COLUMN)
{
return record.strFileName;
}
else if (nColumn == DATE_TIME_COLUMN)
{
return record.dateTime;
}
else if (nColumn == FILE_SIZE_COLUMN)
{
// 之前返回的qint64,现在转换为字符串
return bytesToGBMBKB(record.nSize);
}
return "";
}
default:
return QVariant();
}
return QVariant();
}
QSortFilterProxyModel
QSortFilterProxyModel is used to sort and filter can not be used alone, it is just a "proxy" to provide real data needs QAbstractTableModel, you can override lessThan to achieve their own sorting algorithm.
SortFilterProxyModel::SortFilterProxyModel(QWidget *parent)
: QSortFilterProxyModel(parent)
{
}
SortFilterProxyModel::~SortFilterProxyModel()
{
}
bool SortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
if (!source_left.isValid() || !source_right.isValid())
return false;
if ((source_left.column() == FILE_NAME_COLUMN) && (source_right.column() == FILE_NAME_COLUMN))
{
QVariant leftData = sourceModel()->data(source_left);
QVariant rightData = sourceModel()->data(source_right);
if (leftData.canConvert<QString>() && rightData.canConvert<QString>())
{
QString strLeft = leftData.toString();
QString strRight = rightData.toString();
// 去掉后缀.cpp
if (strLeft.contains("."))
{
int nIndex = strLeft.lastIndexOf(".");
strLeft = strLeft.left(nIndex);
}
if (strRight.contains("."))
{
int nIndex = strRight.lastIndexOf(".");
strRight = strRight.left(nIndex);
}
// 比较大小,如果字符串相同,则比较后面的整形数据
QStringList strLeftList = strLeft.split(" ");
QStringList strRightList = strRight.split(" ");
if ((strLeftList.count() >= 2) && (strRightList.count() >= 2))
{
int nResult = QString::compare(strLeftList.at(0), strRightList.at(0), Qt::CaseInsensitive);
if (nResult == 0)
{
return strLeftList.at(1).toInt() < strRightList.at(1).toInt();
}
else
{
return nResult;
}
}
}
}
return QSortFilterProxyModel::lessThan(source_left, source_right);
}
https://blog.csdn.net/liang19890820/article/details/50782379