16

Question: how to find sub item, in a QTreeView loaded QAbstractItemModel model with model->match() method?

Problem: model->match() can't find sub items, wtf?!

Here is the example:

alt text

As you can see from the picture, I'm trying to expand Layouts sub item with this code:

void Dialog::restoreState(void)
{
    // get list
    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("MainWindow");
    QStringList List = settings.value("ExpandedItems").toStringList();
    settings.endGroup();

    foreach (QString item, List)
    {
        if (item.contains('|'))
            item = item.split('|').last();
        // search `item` text in model
        QModelIndexList Items = model->match(model->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
        if (!Items.isEmpty())
        {
            // Information: with this code, expands ONLY first level in QTreeView
            view->setExpanded(Items.first(), true);
        }
    }
}

Where settings.ini file contains:

[MainWindow]
ExpandedItems=Using Containers, Connection Editing Mode, Form Editing Mode, Form Editing Mode|Layouts

PS: root items successfully expands on start!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mosg
  • 12,041
  • 12
  • 65
  • 87
  • If you are using your own item model, you could simply reimplement match method to fit your needs (as docs says) – Kamil Klimek Jul 20 '10 at 14:28
  • @Kamil Klimek For example, I buy a car, why do I have to install the fifth wheel, if car could run only with four wheels? The other question, if this method really work as docs say... – mosg Jul 21 '10 at 06:32
  • Well, as there is nothing about match method is recursive, i gues it is not, because it's implemented in QAbstractItemModel, witch doesn't know anything about any tree (or other) hierarchy. That's why you HAVE to reimplement match method if you want to make it recursive. – Kamil Klimek Jul 22 '10 at 06:38
  • Oh, and according to you "car"... Well it's better to have fifth (spare) wheel, in case if your other wheel get flat.. – Kamil Klimek Jul 22 '10 at 06:40

3 Answers3

29

Here is the solution:

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            2, // look *
            Qt::MatchRecursive); // look *
  • * Without that argument match() function searches only 1 level
aknuds1
  • 65,625
  • 67
  • 195
  • 317
mosg
  • 12,041
  • 12
  • 65
  • 87
0

Wanted to add to the answer that @mosg gave

The forth parameter is actually the hits parameters. It decides ho many matches one wants to return.

For all matches specify -1 as can be seen here:

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            -1, // any number of hits
            Qt::MatchRecursive); // look *
Thomas N
  • 3
  • 2
0

My working example on QTreeView.

QModelIndexList Indexes = this->ui->treeView->selectionModel()->selectedIndexes();
if(Indexes.count() > 0)
{
    QStandardItemModel *am = (QStandardItemModel*)this->ui->treeView->model();

    QStack<QModelIndex> mis;
    QModelIndex mi = Indexes.at(0);
    while(mi.isValid())
    {
        mis.push(mi);
        mi = mi.parent();
    }

    QStandardItem *si;
    bool FirstTime = true;
    while (!mis.isEmpty())
    {
        mi = mis.pop();
        if(FirstTime)
        {
            FirstTime = false;
            si = am->item(mi.row());
        }
        else
        {
            si = si->child(mi.row());
        }
    }
  // "si" - is selected item
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alatey
  • 371
  • 2
  • 6