#include <QtCore>
#include <QtGui>
{
Q_OBJECT
public:
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());
if(info.isDir() && selectDirs_) return true;
if(info.isFile() && selectFiles_) return true;
return false;
}
private:
bool selectDirs_;
bool selectFiles_;
};
{
Q_OBJECT
public:
Proxy2()
{
model_ = new QFileSystemModel(this);
normalProxy_->setSourceModel(model_);
proxy_ = new fileProxy(this);
proxy_->setSourceModel(model_);
treeView_->setModel(normalProxy_);
treeView_->setRootIndex(normalProxy_->mapFromSource(model_->setRootPath("")));
tableView_->setModel(proxy_);
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)));
layout->addWidget(treeView_);
layout->addWidget(tableView_);
layout->addWidget(dirBox);
layout->addWidget(fileBox);
setLayout(layout);
}
public slots:
{
QModelIndex sourceIndex
= normalProxy_
->mapToSource
(index
);
tableView_->setRootIndex(proxy_->mapFromSource(sourceIndex));
}
private:
QFileSystemModel *model_;
fileProxy *proxy_;
};
{
Proxy2 p2;
p2.show();
return app.exec();
}
#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() && 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();
}
To copy to clipboard, switch view to plain text mode
if(info.isFile() && selectFiles_) return true;
if(info.isFile() && selectFiles_) return true;
To copy to clipboard, switch view to plain text mode
Bookmarks