PDA

View Full Version : Proxy for the QFileSystemModel



stereoMatching
26th July 2012, 12:26
#include <QtCore>
#include <QtGui>

class busyProxy : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit busyProxy(QObject *parent = 0) : QSortFilterProxyModel(parent) {}

bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
QString const &text = sourceModel()->data(index).toString();
//the folder "very big" contains a lot of jpg, and I only want
//to show one of it which name "kkk0.jpg"
if(text == "kkk0.jpg" || text == "kkk1000.jpg" || text == "kkk10.jpg") return true;

if(text == "very big") return true;

return false;
}

bool lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QString const leftString = sourceModel()->data(left).toString();
QString const rightString = sourceModel()->data(right).toString();

qDebug() << leftString << ", " << rightString;

return leftString > rightString;
}
};

//Do nothing, only want to compare with the busyProxy
class lazyProxy : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit lazyProxy(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
};

class testProxyFiles : public QWidget
{
public:
testProxyFiles()
{
QFileSystemModel *model = new QFileSystemModel(this);

lazyProxy *lazyPro = new lazyProxy(this);
lazyPro->setSourceModel(model);

QTableView *sourceView = new QTableView(this);
sourceView->setModel(lazyPro);
QString const rootPath = "E:/file_test/very big";
sourceView->setRootIndex(lazyPro->mapFromSource(model->setRootPath(rootPath)));

QTableView *proxyView = new QTableView(this);
busyProxy *busyPro = new busyProxy(this);
busyPro->setSourceModel(model);
proxyView->setModel(busyPro);
proxyView->setRootIndex(busyPro->mapFromSource(model->setRootPath(rootPath)));

QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(sourceView);
layout->addWidget(proxyView);

setLayout(layout);
}
};


inline int proxyFiles(QApplication &app)
{
testProxyFiles proxy;
proxy.show();

return app.exec();
}

int main( int argc, char **argv )
{
QApplication app( argc, argv );

proxyFiles(app);

return 0;
}


I have several questions :
1 : On line 16~18, do I have other solution to filter out the item of the QFileSystemModel?
The QFileSystemModel would navigate the root first, if I don't return true when the text
equal to "very big", the proxy wouldn't navigate the files inside "very big"

2 : when I show the item by busyProxy or lazyProxy, the indexes on the leftside are weird, how could
I edit them?When I set the QFileSystemModel as the model of QtableView, the indexes looks fine.

3 : the lessThan function never print anything, but why?

4 : Do you have any easy examples for the proxy of QFileSystemModel? I can't find a topic
talk about how to design a proxy for QFileSystemModel

Thanks a lot

Added after 14 minutes:

Besides, how could I ask the view to display all of the drives(win7)?


sourceView->setRootIndex(lazyPro->mapFromSource(model->setRootPath(rootPath)));


What kind of path should I set at the "setRootPath" function?Thanks