1
我正在使用QFileSystemModel與QListview顯示目錄中的所有文件。 我想篩選模型,顯示一個類似文件的某些類別:過濾QFilesystemModel
- 文本文件:* .TXT * .CSV * .TAB
- 音樂:* .MP3 * .FLAC * .OGG
- 的電影:* .AVI * .MKV
我當前的代碼是:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
Filemodel = new QFileSystemModel(this) ;
Filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files) ;
proxy_model = new QSortFilterProxyModel();
proxy_model ->setDynamicSortFilter(true);
proxy_model ->setSourceModel(Filemodel);
proxy_model ->setFilterKeyColumn(0);
ui->Filtered_tbView->setModel(proxy_model) ;
}
(...)
/* combobox event to select file type to filter */
void MainWindow::on_FSFilter_Combo_currentIndexChanged(int index)
{
proxy_model->setFilterWildcard("*.txt"); // just a simple example here
ui->Filtered_tbView->setModel(proxy_model) ;
}
當目錄中存在所有類型的文件時,該代碼不顯示任何內容。
此外,東西我已經試過了是不是對我很好(指針可能會進一步讀者有用):
- setNameFilters:工作得很好,但讓顯示所有文件(未過濾成灰色)
- the Custom Sort/Filter Model Example - >在使用QSortFilterProxyModel時,這個例子有些過於複雜,不能過濾掉文件擴展名,除了使用regexp,在使用像這裏這樣的許多過濾器時,它不是最好的方法。
- 我還發現一個interesting snippet from qt-project但無法找到如何與多個擴展