- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
EDIT2: model.hasChildren(parentIndex)
返回 True
,但 model.rowCount(parentIndex)
返回0
。 QFileSystemModel 只是 PyQt 中的 fubar 吗?
编辑:经过一些调整,如果我使用 QDirModel,这一切都可以完全正常工作。这已被弃用,但也许 QFileSystemModel 尚未在 PyQt 中完全实现?
<小时/>我现在正在学习 Qt 模型/ View 架构,但我发现一些东西并不像我期望的那样工作。我有以下代码(改编自 Qt Model Classes ):
from PyQt4 import QtCore, QtGui
model = QtGui.QFileSystemModel()
parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex) #prints True
print model.data(parentIndex).toString() #prints name of current directory
rows = model.rowCount(parentIndex)
print rows #prints 0 (even though the current directory has directory and file children)
这是 PyQt 的问题吗?我刚刚做错了什么,还是我完全误解了 QFileSystemModel?根据文档, model.rowCount(parentIndex) 应该返回当前目录中的子级数。 (我在 Ubuntu 下使用 Python 2.6 运行它)
QFileSystemModel 文档说它需要一个 Gui 应用程序的实例,因此我也将上述代码放入 QWidget 中,如下所示,但结果相同:
import sys
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
model = QtGui.QFileSystemModel()
parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex)
print model.data(parentIndex).toString()
rows = model.rowCount(parentIndex)
print rows
def main():
app = QtGui.QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
最佳答案
我已经解决了。
使用 QFileSystemModel 而不是 QDirModel 的原因是因为 QFileSystemModel 在单独的线程中从文件系统加载数据。问题是,如果您在构建后尝试打印子级的数量,那么它还没有加载子级。修复上述代码的方法是添加以下内容:
self.timer = QtCore.QTimer(self)
self.timer.singleShot(1, self.printRowCount)
到构造函数的末尾,添加一个 printRowCount 方法,该方法将打印正确的子项数量。唷。
关于python - 我的 QFileSystemModel 在 PyQt 中无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2658467/
我正在寻找的几乎与 QFileSystemModel::setNameFilters() 相反。我想设置过滤器来捕获我不想显示的内容,但让其他所有内容通过。我看不出有什么方法可以遍历和隐藏某些文件,因
我可以扩展 QFileSystemModel 并添加带有文本/图标的新列吗? 问候 最佳答案 我将从子类化模型开始,提供额外的列并向其提供数据。 所以至少我会在这两种情况下重新实现 columnCou
我正在使用带有 QListview 的 QFileSystemModel 来显示目录中的所有文件。我想过滤该模型以显示某些类别的文件,例如: 文本文件:*.txt *.csv *.tab 音乐:*.m
我可以扩展 QFileSystemModel 并添加带有文本/图标的新列吗? 问候 最佳答案 我将从子类化模型开始,提供额外的列并向其提供数据。 所以至少我会在这两种情况下重新实现 columnCou
我正在使用以下过滤器设置我的 QFileSystemModel: QDir::Filters( Dirs|AllDirs|Files|Drives|NoDotAndDotDot|AllEntries
我正在尝试创建一个文件资源管理器,您可以在其中查找文件。找到后,用户应该能够选择他要上传的文件。因此我需要所选文件的路径。 这是我当前的代码: import sys from PyQt4.QtGui
在我的项目中,我有一个 QTreeView 显示我的驱动器上的一个位置。我需要将文件的所有图标更改为自定义图标,但不要管文件夹。 我重新实现了 QFileSystemModel,并且能够更改所有图标。
我正在尝试创建一个显示文件夹内容的 Qt 应用程序(在 Mac OS 中为“Users”文件夹)。这是代码: QFileSystemModel *dirModel = new QFileSystemM
如何像在 QDirModel 中那样使用 QDir::DirsFirst 对 QFileSystemModel 进行排序?QFileSystemModel 没有setSorting 方法。 最佳答案
所以我有一个 TreeView ,如下所示; #QTreeView widget #Shows files in set directory self.treeView = Q
我正在开始 Qt/pySide 编程,并试图实现一个简单的 QListView,以 QFileSystemModel 作为模型。我有这个工作,另外在模型上定义了一个名称过滤器。我想获得 QListVi
我正在尝试使用 QFileSystemModel 和 QTableView 制作一个小文件浏览器。 我想知道是否可以在“修改日期”列中突出显示具有相同值的行,例如,如果我今天修改了两个或多个文件,行以
是否可以使用 QFileSystemModel只列出一个目录内容而不是我计算机中的所有驱动器(在 Windows 中)? 最佳答案 这将是您的 View 的功能,而不是您的模型: http://doc
我正在 qt 中为一些自定义设计文件制作一个文件浏览器。我想将它们的预览加载为缩略图,因此我使用 QIconProvider 将图标返回到我的 QFileSystemModel。 问题是创建 QIco
您好,我已经编写了这段基本代码,试图填充 /Users/ 目录下的文件夹,但我不知道我错过了什么,它没有填充。 import sys from PyQt4 import QtGui from PyQt
首先,这个问题与另一个问题类似QFileSystemModel not updating when files change与本例的主要区别是,我不想每次更新我的文件之一时都替换整个模型。 在现实世界
通过环顾四周,我发现 QFileSysetmModel 中的 QFileIconProvider 会大大减慢速度。在我的特殊情况下,我根本不需要它,但我不知道如何轻松地禁用/删除它而不导致崩溃。这是我
我使用 QFileSystemModel 通过 QTreView 表示文件结构。一切正常,但我需要在树的某个级别添加额外的行。例如现在是: -根 --row1 --row2 --row3 所有这些行从
我遇到 QFileSystemModel 无法显示文件更改的问题。当文件第一次创建时,它会立即显示出来。但是当文件本身发生变化时,大小和时间戳不会更新。我已经多次尝试强制模型更新,但没有真正成功。我所
我有一个 QFileSystemModel 用作 QTreeView 中的模型。这个工作正常,现在我有一些目录的名称。我想将其隐藏在 QTreeview 中。 QString strCompleteD
我是一名优秀的程序员,十分优秀!