views:

111

answers:

2

Hello.

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!

+1  A: 

Sorry it's not an answer,

1- Does "QVariant::toStringList()" return your desired StringList! In other word, how does it found what character(',' or '|' or ' ') is used for splitting?

2- If you just want expanding all item, why don't you use QTreeView::expandAll() function?

Razi
It's better, if you need to ask, just post your questions in comments, that is not the good practice to answer with a questions... 1. foreach() returns me strings, for example "ABC", "BCD|CDE" and "DEF"; after that I checked them with split('|') function; 2. I don't need expand all of them, I just want to expand last user expand items (recursively)
mosg
@mosg: sorry but "add comment" button is not visible follow your question and also follow your answer!! I think it's because I'm very new here. What do you suggest, I delete this answer, because I don't have enough "reputation" to vote up your answer.
Razi
Now you have ;) Also have a look at http://stackoverflow.com/faq Good luck!
mosg
+1  A: 

Here is the solution:

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            2, // look *
            Qt::MatchRecursive); // look *
  • * with out that argument match() function searches only 1 level
mosg