-
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QWidget>
- #include <QTreeWidget>
- #include <QBrush>
- #include <QDebug>
- namespace Ui {
- class Widget;
- }
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
- void searchChild(QTreeWidgetItem *cItem, const QString &txt);
- void parentExpand(QTreeWidgetItem *item);
- private slots:
- void on_pushButton_clicked();
- void on_lineEdit_editingFinished();
- private:
- Ui::Widget *ui;
- QBrush m_itemBrush;
- };
- #endif // WIDGET_H
.cpp文件
- #include "widget.h"
- #include "ui_widget.h"
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- m_itemBrush = ui->treeWidget->topLevelItem(0)->background(0);
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::searchChild(QTreeWidgetItem *cItem, const QString &txt)
- {
- int childCount = cItem->childCount();
- for (int i = 0; i < childCount; ++i) {
- QTreeWidgetItem *item = cItem->child(i);
- if (-1 != item->text(0).indexOf(txt)) {
- item->setExpanded(true);
- item->setBackground(0,Qt::green);
- parentExpand(item);
- } else {
- item->setBackground(0, m_itemBrush);
- }
- searchChild(item, txt);
- }
- }
- void Widget::parentExpand(QTreeWidgetItem *item)
- {
- if (item->parent() != NULL) {
- QTreeWidgetItem *pItem = item->parent();
- if (! pItem->isExpanded()) {
- pItem->setExpanded(true);
- }
- parentExpand(pItem);
- }
- }
- void Widget::on_pushButton_clicked()
- {
- ui->treeWidget->collapseAll();
- QString txt = ui->lineEdit->text();
- bool isEmptyTxt = txt.isEmpty();
- bool isEmptyTree = ui->treeWidget->topLevelItemCount(); //非零个为true
- if (isEmptyTxt) {
- QTreeWidgetItemIterator it(ui->treeWidget);
- while(*it) {
- (*it)->setBackground(0, m_itemBrush);
- it++;
- }
- return;
- }
- if ( ! isEmptyTree) { return; }
- #if 0
- int count = ui->treeWidget->topLevelItemCount();
- for (int i =0; i < count; ++i) {
- QTreeWidgetItem *cItem = ui->treeWidget->topLevelItem(i);
- if (-1 != cItem->text(0).indexOf(txt)) {
- cItem->setBackground(0,Qt::green);
- } else {
- cItem->setBackground(0, m_itemBrush);
- }
- searchChild(cItem, txt);
- }
- #else
- QTreeWidgetItemIterator it(ui->treeWidget);
- while(*it) {
- QString str = (*it)->text(0);
- if (-1 != str.indexOf(txt)) {
- QTreeWidgetItem *pItem = (*it)->parent();
- if (pItem != NULL) {
- bool isExpanded = pItem->isExpanded();
- if (! isExpanded){
- ui->treeWidget->expandItem(pItem);
- }
- }
- (*it)->setBackground(0,Qt::green);
- parentExpand(*it);
- } else{
- (*it)->setBackground(0, m_itemBrush);
- }
- it++;
- }
- #endif
- }
- void Widget::on_lineEdit_editingFinished()
- {
- on_pushButton_clicked();
- }
main文件
- #include "widget.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }