[英]background color for certain items(rows) in QFileSystemModel QTreeView
[英]Hide items of a QFileSystemModel/QTreeView via indexes using a specific rule
我正在使用QTreeView + QFileSystemModel在我的Qt程序中顯示文件夾的內容。
現在我想隱藏該視圖的特定項目。 顯示規則不基於文件名,因此我不能使用setNameFilters()。 我所擁有的是一個簡單的QModelIndex列表,其中包含我要隱藏的所有項目。 有沒有一種方法只使用此列表過濾視圖?
在我的研究中,我遇到了QSortFilterProxyModel類,但我無法想象如何使用它來實現我想要的。 任何幫助,將不勝感激。
子類QSortFilterProxyModel
並覆蓋方法filterAcceptsRow
以設置過濾器邏輯。
例如,要過濾當前用戶的寫入權限:
class PermissionsFilterProxy: public QSortFilterProxyModel
{
public:
PermissionsFilterProxy(QObject* parent=nullptr): QSortFilterProxyModel(parent)
{}
bool filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
QFileDevice::Permissions permissions = static_cast<QFileDevice::Permissions>(index.data(QFileSystemModel::FilePermissions).toInt());
return permissions.testFlag(QFileDevice::WriteUser); // Ok if user can write
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFileSystemModel* model = new QFileSystemModel();
model->setRootPath(".");
QTreeView* view = new QTreeView();
PermissionsFilterProxy* proxy = new PermissionsFilterProxy();
proxy->setSourceModel(model);
view->setModel(proxy);
view->show();
return app.exec();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.