Weird behavior when filter QFileSystemModel by proxy



  • [code]
    #include <QtCore>
    #include <QtGui>

    class fileProxy : public QSortFilterProxyModel
    {
    Q_OBJECT

    public:
    explicit fileProxy(QObject *parent = 0) : QSortFilterProxyModel(parent), selectDirs_(true), selectFiles_(true) {}

    public slots:
    void setSelectDirs(bool enable) { selectDirs_ = enable; }
    void setSelectFiles(bool enable) { selectFiles_ = enable; }

    protected:
    bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
    {
    QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
    QFileSystemModel source = static_cast<QFileSystemModel>(sourceModel());
    QFileInfo info = source->fileInfo(index);

        if(info.isDir() && selectDirs_) return true;
    
        if(info.isFile&#40;&#41; && selectFiles_) return true;
    
        return false;
    }
    

    private:
    bool selectDirs_;
    bool selectFiles_;

    };

    class Proxy2 : public QWidget
    {
    Q_OBJECT

    public:
    Proxy2()
    {
    model_ = new QFileSystemModel(this);

        normalProxy_ = new QSortFilterProxyModel(this);
        normalProxy_->setSourceModel(model_);
    
        proxy_ = new fileProxy(this);
        proxy_->setSourceModel(model_);
    
        treeView_ = new QTreeView(this);
        treeView_->setModel(normalProxy_);
        treeView_->setRootIndex(normalProxy_->mapFromSource(model_->setRootPath("")));
    
        tableView_ = new QTableView(this);
        tableView_->setModel(proxy_);
    
        connect(treeView_, SIGNAL(clicked(QModelIndex)), this, SLOT(changeTableRootIndex(QModelIndex)));
    
        QCheckBox *dirBox = new QCheckBox(this);
        QCheckBox *fileBox = new QCheckBox(this);
    
        dirBox->setText("Dir");
        fileBox->setText("file");
        dirBox->setCheckState(Qt::Checked);
        fileBox->setCheckState(Qt::Checked);
        connect(dirBox, SIGNAL(clicked(bool)), proxy_, SLOT(setSelectDirs(bool)));
        connect(fileBox, SIGNAL(clicked(bool)), proxy_, SLOT(setSelectFiles(bool)));
    
        QHBoxLayout *layout = new QHBoxLayout(this);
        layout->addWidget(treeView_);
        layout->addWidget(tableView_);
        layout->addWidget(dirBox);
        layout->addWidget(fileBox);
    
        setLayout(layout);
    }
    

    public slots:
    void changeTableRootIndex(QModelIndex const &index)
    {
    QModelIndex sourceIndex = normalProxy_->mapToSource(index);
    tableView_->setRootIndex(proxy_->mapFromSource(sourceIndex));
    }

    private:
    QFileSystemModel *model_;
    QSortFilterProxyModel *normalProxy_;
    fileProxy *proxy_;
    QTableView *tableView_;
    QTreeView *treeView_;
    };

    inline int testProxy2(QApplication &app)
    {
    Proxy2 p2;
    p2.show();

    return app.exec();
    

    }
    [/code]

    I hope the fileProxy will filter the files according to the checkstate of the check box
    But the results are pretty weird

    1 : It would not filter out the files or directories instantly, I have to switch between different directories or drives
    of the treeView
    2 : Even I did switch the directories or drives, there is no guarantee the fileProxy will filter out the directories or
    files

    What kind of mistakes do I make?
    Thanks a lot



  • I narrow the problem a little bit
    If I remove line 22, neglect all of the check box and change the codes to

    [code]
    if(info.isFile() && selectFiles_) return true;
    [/code]

    The TableView will never show anything
    How could I show the files(only files)to the users?
    Thanks a lot


Log in to reply
 

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