本文主要是介绍QTreeWidget实现的目录树无法按照Windows系统的默认排序那样,有数字时,按数字的值排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
经过测试发现QTreeWidget 的排序是安装字符串比较大小来排序的,而Widows下的排序对于有数字的是按数值大小在排序,
这里就重载了QTreeWidgetItem的小于操作符函数
.h
class CTreeWidgetItemEx : public QTreeWidgetItem
{
public:
explicit CTreeWidgetItemEx(QTreeWidget *view, int type = Type);
CTreeWidgetItemEx(QTreeWidget *view, const QStringList &strings, int type = Type);
CTreeWidgetItemEx(QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
explicit CTreeWidgetItemEx(QTreeWidgetItem * parent = 0, int type = QTreeWidgetItem::Type);
~CTreeWidgetItemEx();
bool operator<(const QTreeWidgetItem & other) const;
};
.cpp
CTreeWidgetItemEx::CTreeWidgetItemEx(QTreeWidgetItem * parent /*= 0*/, int type /*= QTreeWidgetItem::Type*/)
: QTreeWidgetItem(parent, type)
{
}
CTreeWidgetItemEx::CTreeWidgetItemEx(QTreeWidget *view, int type /*= Type*/) : QTreeWidgetItem(view,type)
{
}
CTreeWidgetItemEx::CTreeWidgetItemEx(QTreeWidget *view, const QStringList &strings, int type /*= Type*/) : QTreeWidgetItem(view,strings,type)
{
}
CTreeWidgetItemEx::CTreeWidgetItemEx(QTreeWidget *view, QTreeWidgetItem *after, int type /*= Type*/) : QTreeWidgetItem(view,after)
{
}
CTreeWidgetItemEx::~CTreeWidgetItemEx()
{
}
bool CTreeWidgetItemEx::operator<(const QTreeWidgetItem & other) const
{
QString strText = text(0);
QString ostrText = other.text(0);
const wchar_t *wstrText = reinterpret_cast<const wchar_t*>(strText.utf16());
const wchar_t *wstrOtherText = reinterpret_cast<const wchar_t*>(ostrText.utf16());
int result = StrCmpLogicalW((wchar_t*)wstrText,(wchar_t*)wstrOtherText);
return result == -1;
}
//设置升序
m_leftTreeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);
m_leftTreeWidget->setSortingEnabled(true);
这篇关于QTreeWidget实现的目录树无法按照Windows系统的默认排序那样,有数字时,按数字的值排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!