QSortFilterProxyModel
-
I have a Qtreeview displaying a custom QSortFilterProxyModel based on the QFileSystemModel. I need to filter out rows within my root directory. What can I use to filter only files within the rootdirectory that I set (QModelIndex? cast my sourcemodel to QFileSystemModel and compare file name?)? What would be the ideal way to implement this?
-
Andre, can you provide code snippet? Let's say I have the following directory and I wanted a filter to do the folders below:
@
TestSuite <--- Root Directory
-- common <--- need to filter this folder
-- tools <--- need to filter this folder
-- test1 <--- keep this folder
-- test2 <--- keep this folder
L cfg <--- need to filter this subfolder
L testa <--- keep this subfolder
L testb <--- keep this subfolder
@Basically the logic for my filter will be 1.) Child of root directory and 2) Subdirectory folder must start with test.
-
Andre, I was not trying to copy your source by any means. I was hoping your example can help answer some of things I am unsure how to implement efficiently. The following is what I currently have:
@
bool CheckableProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{
QModelIndex index0 = sourceModel()->index(source_row, COL_FILE_NAME, source_parent);
QString str = ((QFileSystemModel)sourceModel()).fileName(index0);qDebug() << "filterAcceptsRow 0: " << str; if (str == "/") return true; str = ((QFileSystemModel)sourceModel()).filePath(index0); if (str.contains(root)) { qDebug() << "filterAcceptsRow 1: " << str; str = str.remove(0, root.length()); qDebug() << "filterAcceptsRow 2: " << str; if (str == "" || str.startsWith("test_")) return true; } return false;
}
@Can you help clarify the following:
1.) I am currently using a fixed string for my root directory comparison. Is there a way for me to retrieve it from my source model?
2.) How do I iterate through the subdirectories for line 21 and filter out the rows that starts with "test_"You help is greatly appreciated!