Problem with using proxy model!



  • Hello. I use Qt 5.2.1. Can't fix one problem for a month.

    Here's the deal. I have source_model->proxy_model->tree view (QFileSystemModel->QSortFilterProxyModel->QTreeView) and few combo boxes. Some filtration occurs in source model, some in proxy. Data (file system) is shown in treeview when user click on BUILD button. Filter in source model works well. In proxy there are 2 filters.
    This is the redefinition of filterAcceptsRow function:
    @
    bool FilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    {
    if (filterAcceptsRowItself(sourceRow, sourceParent))
    return true;
    //accept if any of the children is accepted on it's own merits
    if (hasAcceptedChildren(sourceRow, sourceParent)) {
    return true;
    }
    return false;
    }

    bool FilterProxyModel::filterAcceptsRowItself(int sourceRow, const QModelIndex &sourceParent) const
    {
    QModelIndex index0 = sourceModel()->index(sourceRow, 3, sourceParent); // получаю индекс по столбцу времени после.модиф.
    QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); // размер файла

    QDateTime now = QDateTime::currentDateTime();
    QDateTime fileMtime = QDateTime::fromString((sourceModel()->data(index0)).toString(),Qt::LocalDate).addYears(100);
    
    
    bool timeFilter = mdtime < fileMtime.daysTo(now);
    bool sizeFilter = filterFileSize(sourceModel()->data(index1).toString());
    
    return (timeFilter && sizeFilter);
    

    }

    bool FilterProxyModel::hasAcceptedChildren(int sourceRow, const QModelIndex &sourceParent) const
    {
    QModelIndex item = sourceModel()->index(sourceRow,0,sourceParent);
    if (!item.isValid()) {
    //qDebug() << "item invalid" << sourceParent << sourceRow;
    return false;
    }

    //check if there are children
    int childCount = item.model()->rowCount(item);
    if (childCount == 0)
        return false;
    
    for (int i = 0; i < childCount; ++i) {
        if (filterAcceptsRowItself(i, item))
            return true;
        //recursive call -> NOTICE that this is depth-first searching, you're probably better off with breadth first search...
        if (hasAcceptedChildren(i, item))
            return true;
    }
    return false;
    

    }
    @
    Filtration rules are set in filterAcceptsRowItself(). Function bool filterFileSize() takes string with file size, parses it and returns true, if file satisfies the filter. It works well too.

    Problem occurs when index of the model gets in view:
    @
    filterModel->setSourceModel(treemodel);
    // ...
    ui->treeView->setModel(filterModel);
    QModelIndex ind = filterModel->mapFromSource(treemodel->setRootPath(path)); // path считывается из виджета lineEdit
    if (!ind.isValid()) return;
    ui->treeView->setRootIndex(ind);
    @
    If after filtration model still has some data in it then everything is ok and data are shown in view correctly. But if model becomes empty after filtration something goes wrong:
    1)or view displayed empty (as it should, cuz filtration returned false)

    1. or "/" is displayed (i guess it means that index was lost or smith like that)
      In first case (after it), if i change the filter value so that the data in the view appeared, then my program breaks. And not in my functions, but in exec() cycle. Somewhere on assembler commands when trying MOV "smth" "smwhr".
      In second case, after next push on BUILD button there is a chance that the correct data will displayed.

    Maybe someone tells me what i do wrong and why index lost after filtering.

    Thank you anyway.



  • no one can help???


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.