通过重载比较符来完成,不过还存在一定得缺陷
class gwDataDictionaryTreeItem : public QTreeWidgetItem
{
public:
explicit gwDataDictionaryTreeItem(QTreeWidget *view, const QStringList &strings, int type = Type);
~gwDataDictionaryTreeItem();
bool operator<(const QTreeWidgetItem & other) const;
};
gwDataDictionaryTreeItem::gwDataDictionaryTreeItem(QTreeWidget *view, const QStringList &strings, int type)
: QTreeWidgetItem(view, strings, type)
{
}
gwDataDictionaryTreeItem::~gwDataDictionaryTreeItem()
{
}
bool gwDataDictionaryTreeItem::operator<(const QTreeWidgetItem & other) const
{
//这里默认将所有的都当作整形来排序,若要扩展需要修改
int column = treeWidget()->sortColumn();
unsigned int a = text(column).toUInt();
unsigned int b = other.text(column).toUInt();
return (a < b);
}
QTreeWidget* m_pTree;
gwDataDictionaryTreeItem *pRoot = new gwDataDictionaryTreeItem(m_pDomainTree, strList);
pRoot->setData(0, gwValueGroupTreeItemRole, qVariantFromValue(1));
QIcon icon = QIcon(RESICON_GROUPVALUE);
pRoot->setIcon(0, icon);
m_pTree->setCurrentItem(pRoot);
m_pTree->sortItems(1, Qt::AscendingOrder);