利用C++11的function和bind功能,實(shí)現(xiàn)QStandardItemModel的通用遍歷函數(shù)
在使用Qt的樹形視圖和表格視圖QTableView和QTreeView時(shí),經(jīng)常需要遍歷所有條目,每種功能都寫一個(gè)遍歷函數(shù)既麻煩又不符合編程最簡原則,因此,寫一個(gè)通用的遍歷函數(shù)是很必要的(類似于std::for_each),對于遍歷時(shí)實(shí)現(xiàn)的功能不一樣,可以通過函數(shù)指針來代替需要實(shí)現(xiàn)的功能。
在講這個(gè)之前,需要了解std::function和std::bind,具體可以自行谷歌。
可參考:http://blog.csdn.net/qq575787460/article/details/8531397
由于這個(gè)遍歷函數(shù)可能經(jīng)常用到,因此可以寫一個(gè)類的靜態(tài)函數(shù)或者是全局函數(shù)。
下面把這個(gè)功能封裝在一個(gè)類里
類的頭文件如下:
#include#includeclass?QStandardItemModelEx
?{
?public:
?????QStandardItemModelEx(){}
?????~QStandardItemModelEx(){}
?????///
?????///?brief?callback_ergodicFun_ptr?回調(diào)函數(shù)指針,bool?f(QStandardItem*),bool用于決定是否繼續(xù),如果為true就繼續(xù)遞歸,如果為false就停止遞歸
?????///
?????typedef?std::functioncallback_ergodicFun_ptr;
?????//typedef?void(*callback_ergodicFun_ptr)(QStandardItem*);
?????static?void?ergodicAllItem(QStandardItemModel*?model,callback_ergodicFun_ptr?pFun);
?????static?bool?ergodicItem(QStandardItem*?item,callback_ergodicFun_ptr?pFun);
?private:
?};
這里使用C++11的新特性function(環(huán)境 VS2010),定義一個(gè)函數(shù)指針用于回調(diào)實(shí)現(xiàn)功能。
函數(shù)ergodicAllItem用于遍歷所有項(xiàng)目,ergodicItem用于遍歷項(xiàng)目下的所有子項(xiàng)目?;卣{(diào)函數(shù)返回bool用于決定是否繼續(xù),如果為true就繼續(xù)遍歷,如果為false就停止遍歷
為了簡單起見,下面用遞歸實(shí)現(xiàn)遍歷(ergodicItem是尾遞歸函數(shù))
?
void?QStandardItemModelEx::ergodicAllItem(QStandardItemModel*?model,callback_ergodicFun_ptr?pFun)
{
????int?rows?=?model->rowCount();
????int?column?=?model->columnCount();
????for?(int?i=0;i<rows;++i)
????{
????????for(int?j=0;jitem(i,j);
????????????if?(item)
????????????{
????????????????if(!ergodicItem(item,pFun))
????????????????????return;
????????????}
????????}
????}
}
bool?QStandardItemModelEx::ergodicItem(QStandardItem*?item,callback_ergodicFun_ptr?pFun)
{
????int?rows?=?item->rowCount();
????int?column?=?item->columnCount();
????for?(int?i=0;i<rows;++i)
????{
????????for(int?j=0;jchild(i,j);
????????????if?(childItem)
????????????{
????????????????if(!ergodicItem(childItem,pFun))
????????????????????return?false;
????????????}
????????}
????}
????return?pFun(item);
}
用這兩個(gè)函數(shù)就可以遍歷所有的項(xiàng)目了,而遍歷時(shí)需要的動(dòng)作,就通過回調(diào)函數(shù)實(shí)現(xiàn)。
回調(diào)函數(shù)即可寫在類里,也可以寫為全局函數(shù),若除了QStandardItem*還需要?jiǎng)e的參數(shù),那么可以用std::bind幫忙。
下面舉一個(gè)例子
例子功能是通過關(guān)鍵字高亮條目。在使用QTreeView或QTableView時(shí),在條目多的情況下,需要搜索某些條目,對搜索結(jié)果進(jìn)行高亮,這時(shí),就需要遍歷整個(gè)View里的條目,對符合的條目進(jìn)行高亮
遍歷的函數(shù)已經(jīng)寫好,現(xiàn)在缺少的是回調(diào)的函數(shù),由于遍歷的函數(shù)使用的函數(shù)指針是std::function,因此回調(diào)函數(shù)即可寫為全局函數(shù)也可以寫為類成員函數(shù)
回調(diào)函數(shù)聲明:
bool?callback_hightLightItem(QStandardItem*?item,const?QStringList?keys);
回調(diào)函數(shù)實(shí)現(xiàn):
bool?callback_hightLightItem(QStandardItem*?item,const?QStringList?keys)
{???
????QString?str?=?item->text();
????if?(is_match_string(str,keys))
????{
????????item->setData(QVariant(QColor(237,100,100,180)),Qt::BackgroundRole);
????}
????else
????{
????????item->setData(QVariant(),Qt::BackgroundRole);
????}
????return?true;
}
使用QStandardItem的setData函數(shù),設(shè)置角色為Qt::BackgroundRole,即可設(shè)置背景顏色。
實(shí)現(xiàn)函數(shù)中is_match_string函數(shù)是用來檢測是否符合關(guān)鍵字,如果符合關(guān)鍵字,就返回true,符合關(guān)鍵字就給條目背景賦予不同的顏色,否則就把顏色消除。
回調(diào)函數(shù)返回true,意味著一直遍歷,直到所有遍歷完成。
通過按鈕pushButton_search來觸發(fā)搜索功能。pushButton_search函數(shù)的槽如下:void?MainWindow::on_pushButton_search_clicked()
{
??? QString str?=?ui->lineEdit_search->text();//獲取關(guān)鍵字
????if?(str.isEmpty())
????????return;
? ??QStringList keyWords?=?str.split(QString(" "));
? ? StandardItemModel::ergodicAllItem(
? ? ? ? qobject_cast





