- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要显示特定目录的 QTreeView,我想让用户可以使用 RegExp 过滤文件。
据我了解 Qt 文档,我可以使用标题中提到的类来实现这一点:
// Create the Models
QFileSystemModel *fileSystemModel = new QFileSystemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
// Set the Root Path
QModelIndex rootModelIndex = fileSystemModel->setRootPath("E:\\example");
// Assign the Model to the Proxy and the Proxy to the View
proxyModel->setSourceModel(fileSystemModel);
ui->fileSystemView->setModel(proxyModel);
// Fix the TreeView on the Root Path of the Model
ui->fileSystemView->setRootIndex(proxyModel->mapFromSource(rootModelIndex));
// Set the RegExp when the user enters it
connect(ui->nameFilterLineEdit, SIGNAL(textChanged(QString)),
proxyModel, SLOT(setFilterRegExp(QString)));
启动程序时,TreeView 会正确固定到指定目录。但是一旦用户更改了 RegExp,TreeView 似乎就忘记了它的 RootIndex。删除 RegExp LineEdit 中的所有文本(或输入类似“.”的 RegExp)后,它再次显示所有目录(在 Windows 上这意味着所有驱动器等)
我做错了什么? :/
最佳答案
我收到了 Qt 邮件列表的回复,其中解释了这个问题:
What I think is happening, is that as soon as you start filtering, the index you use as your root does no longer exist. The view then resets to an invalid index as the root index. The filtering works on the whole model tree, not just on the part you see if you start to enter your filter!
I think you are going to need a modified proxy model to do what you want. It should only apply the filtering on items under your root path, but let the root path itself (and everything else) alone.
所以在子类化 QSortFilterProxyModel 和一些 parent() 函数 filterAcceptsRow() 检查之后,这现在确实按预期工作了!
关于c++ - QTreeView、QFileSystemModel、setRootPath 和 QSortFilterProxyModel 以及用于过滤的 RegExp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3212392/
我对这里的区别是什么以及为什么一个有效而另一个无效感到困惑。谁能解释一下? //The string to search through var str = "This is a string /*
我很困惑这里有什么区别以及为什么一个有效而另一个无效。有人能解释一下吗? //The string to search through var str = "This is a string /* w
概述 RegExp 的构造函数创建了一个正则表达式对象,用模式来匹配文本。 有关正则表达式介绍,请阅读JavaScript指南中的正则表达式章节。 语法 文字和构造符号是可能的: /patt
在我的数据库中,我有一个公司表。该表有一个名为 tags 的字段,其中包含以下内容: Furniture Retail E-commerce B2C Home & Furniture Consumer
var str='The_Andy_Griffith_Show'; // string to perform replace on var regExp1=/\s|[A-Z]/g; var reg
我正在为 VBA 编写一个脚本,用于 Outlook 2013,它使用正则表达式,我发现的每个示例似乎都使用 Set regex = New RegExp创建一个 RegExp 对象。当我尝试这个时,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
在“How do I removing URLs from text?”中建议使用以下代码: require 'uri' #... schemes_regex = /^(?:#{ URI.scheme
所以我有一个 RegExp regex =/asd/ 我将它作为 key 存储在我的键值对存储系统中。 所以我说 str = String(regex) 返回 "/asd/"。 现在我需要将该字符串转
谁能提供一些例子来解释 regexp.Compile 之间的区别?和 regexp.CompilePOSIX ?我阅读了文档。但是我无法得到直观的理解。 最佳答案 Perl 和 POSIX 兼容的正则
我目前正在学习 SQL 并使用 SSMS 2017。我不明白为什么在使用 REGEXP 语法时出现错误,它似乎适用于其他任何人: SELECT * FROM List WHERE Name REGEX
我有一个包含文本的 emacs 缓冲区 a1b2c3 使用正则表达式构建器,我创建了正则表达式 "b\\(2\\)" 并且可以看到匹配突出显示(b2,2 的颜色不同)。 但是,当我将表达式粘贴到 re
这个问题已经有答案了: JavaScript: using constructor without operator 'new' (2 个回答) 已关闭 7 年前。 RegExp('hi') 和有什么
我的正则表达式是这样的: ((?:[a-z][a-z0-9_]*)).*?(\d+).*?((?:[a-z][a-z0-9_]*)).*?(\d+).*?([a-z]) 如果我将其作为 MySQL R
我对基准测试和功能感兴趣?是否有理由使用 Jakarta 正则表达式? 最佳答案 似乎没有什么理由。但是除了 Jakarta 图书馆之外,还有其他一些有趣的图书馆。此链接提供了一些有关性能和 perl
如果声明了 SPEC Env,我将尝试有条件地加载我的测试: var context = null if (process.env.SPEC) { context = require.contex
我尝试为 emacs 编写一些新的对齐规则,但发现这种奇怪且不一致的行为。当前缓冲区内容: "some thing" like => this hello => world and => aga
这个问题在这里已经有了答案: Is gcc 4.8 or earlier buggy about regular expressions? (3 个答案) 关闭 8 年前。 我尝试使用 C++11
我遇到了两个错误,都与编码有关并且都相关。 我在启动 WEBrick 时遇到的第一个错误(技术上是警告): /Users/USERNAME/example/config/initializers/bb
我有一个 almost-json文件。估计1000 行。这是其中的一部分: level: { 1: { cost: 200,
我是一名优秀的程序员,十分优秀!