节点之精确搜索

    1. #ifndef WIDGET_H  
    2. #define WIDGET_H  
    3.   
    4. #include <QWidget>  
    5. #include <QTreeWidget>  
    6. #include <QBrush>  
    7. #include <QDebug>  
    8.   
    9. namespace Ui {  
    10. class Widget;  
    11. }  
    12.   
    13. class Widget : public QWidget  
    14. {  
    15.     Q_OBJECT  
    16.   
    17. public:  
    18.     explicit Widget(QWidget *parent = 0);  
    19.     ~Widget();  
    20.     void searchChild(QTreeWidgetItem *cItem, const QString &txt);  
    21.     void parentExpand(QTreeWidgetItem *item);  
    22.   
    23. private slots:  
    24.     void on_pushButton_clicked();  
    25.   
    26.     void on_lineEdit_editingFinished();  
    27.   
    28. private:  
    29.     Ui::Widget *ui;  
    30.     QBrush m_itemBrush;  
    31. };  
    32.   
    33. #endif // WIDGET_H  

    .cpp文件
[cpp]  view plain  copy
  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3.   
  4. Widget::Widget(QWidget *parent) :  
  5.     QWidget(parent),  
  6.     ui(new Ui::Widget)  
  7. {  
  8.     ui->setupUi(this);  
  9.     m_itemBrush =  ui->treeWidget->topLevelItem(0)->background(0);  
  10. }  
  11.   
  12. Widget::~Widget()  
  13. {  
  14.     delete ui;  
  15. }  
  16.   
  17. void Widget::searchChild(QTreeWidgetItem *cItem, const QString &txt)  
  18. {  
  19.     int childCount = cItem->childCount();  
  20.     for  (int i = 0; i < childCount; ++i) {  
  21.         QTreeWidgetItem *item = cItem->child(i);  
  22.         if (-1 != item->text(0).indexOf(txt)) {  
  23.             item->setExpanded(true);  
  24.             item->setBackground(0,Qt::green);  
  25.             parentExpand(item);  
  26.         } else {  
  27.             item->setBackground(0, m_itemBrush);  
  28.         }  
  29.         searchChild(item, txt);  
  30.     }  
  31. }  
  32.   
  33. void Widget::parentExpand(QTreeWidgetItem *item)  
  34. {  
  35.     if (item->parent() != NULL) {  
  36.         QTreeWidgetItem *pItem = item->parent();  
  37.         if (! pItem->isExpanded()) {  
  38.             pItem->setExpanded(true);  
  39.         }  
  40.         parentExpand(pItem);  
  41.     }  
  42. }  
  43.   
  44. void Widget::on_pushButton_clicked()  
  45. {  
  46.     ui->treeWidget->collapseAll();  
  47.     QString txt = ui->lineEdit->text();  
  48.     bool isEmptyTxt = txt.isEmpty();  
  49.     bool isEmptyTree = ui->treeWidget->topLevelItemCount(); //非零个为true  
  50.     if (isEmptyTxt) {  
  51.         QTreeWidgetItemIterator it(ui->treeWidget);  
  52.         while(*it) {  
  53.             (*it)->setBackground(0, m_itemBrush);  
  54.             it++;  
  55.         }  
  56.         return;  
  57.     }  
  58.     if ( ! isEmptyTree) { return; }  
  59.   
  60.   
  61. #if 0  
  62.     int count = ui->treeWidget->topLevelItemCount();  
  63.     for (int i =0; i < count; ++i) {  
  64.         QTreeWidgetItem *cItem = ui->treeWidget->topLevelItem(i);  
  65.         if (-1 != cItem->text(0).indexOf(txt)) {  
  66.             cItem->setBackground(0,Qt::green);  
  67.         } else {  
  68.             cItem->setBackground(0, m_itemBrush);  
  69.         }  
  70.         searchChild(cItem, txt);  
  71.     }  
  72. #else  
  73.   
  74.     QTreeWidgetItemIterator it(ui->treeWidget);  
  75.     while(*it) {  
  76.         QString str = (*it)->text(0);  
  77.         if (-1 != str.indexOf(txt)) {  
  78.             QTreeWidgetItem *pItem = (*it)->parent();  
  79.             if (pItem !=  NULL) {  
  80.                 bool isExpanded = pItem->isExpanded();  
  81.                 if (! isExpanded){  
  82.                     ui->treeWidget->expandItem(pItem);  
  83.                 }  
  84.             }  
  85.             (*it)->setBackground(0,Qt::green);  
  86.             parentExpand(*it);  
  87.         } else{  
  88.             (*it)->setBackground(0, m_itemBrush);  
  89.         }  
  90.         it++;  
  91.     }  
  92. #endif  
  93. }  
  94.   
  95. void Widget::on_lineEdit_editingFinished()  
  96. {  
  97.     on_pushButton_clicked();  
  98. }  

main文件

[cpp]  view plain  copy
  1. #include "widget.h"  
  2. #include <QApplication>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.     Widget w;  
  8.     w.show();  
  9.   
  10.     return a.exec();  
  11. }  

猜你喜欢

转载自blog.csdn.net/u014746838/article/details/79328683