Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Problem with using proxy model!
Forum Updated to NodeBB v4.3 + New Features

Problem with using proxy model!

Scheduled Pinned Locked Moved C++ Gurus
2 Posts 1 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jasmine_
    wrote on last edited by
    #1

    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)
    2) 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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jasmine_
      wrote on last edited by
      #2

      no one can help???

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved