QTreeWidget过滤器和finditems不起作用

时间:2013-05-11 09:49:23

标签: c++ qt4 qt5

我的代码不起作用。我希望通过文本(RegExp)过滤QTreeWidget - 只有0列

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTreeWidgetItem>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //declared in this class private: QList<QTreeWidgetItem*> items;
    for(int i=0; i<=10; i++)
    {
        QTreeWidgetItem *item1 = new QTreeWidgetItem();
        item1->setText(0,QString("Name %1").arg(i));
        item1->setText(1,"Value 1");
        item1->setText(2,"Value 2");
        items.push_back(item1);
    }

    ui->treeWidget->addTopLevelItems(items);
}

void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
    //filter by Name
    QList<QTreeWidgetItem *> ItemsFinded;
    ui->treeWidget->clear();
    ui->treeWidget->addTopLevelItems(items);
    ItemsFinded = ui->treeWidget->findItems(arg1, Qt::MatchRegExp,0);

    ui->treeWidget->clear();
    ui->treeWidget->addTopLevelItems(ItemsFinded);

    qDebug("Finded: %d / %d", ItemsFinded.count(), items.count());
}

有什么问题?为什么finditems总是在ItemsFinded中返回0个计数。

0 个答案:

没有答案