Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 247
mosg Avatar asked Jul 20 '10 13:07

mosg


1 Answers

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
like image 120
mosg Avatar answered Nov 15 '22 18:11

mosg