qtqt4qabstractitemmodel

HowTo find Subitem in QAbstractItemModel and QTreeView class?


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!


Solution

  • Here is the solution:

    QModelIndexList Items = model->match(
                model->index(0, 0),
                Qt::DisplayRole,
                QVariant::fromValue(item),
                2, // look *
                Qt::MatchRecursive); // look *