Python QFileSystemModel QTableView日期修改高亮显示
我正在尝试使用QFileSystemModel和QTableView制作一个小文件浏览器 我想知道是否有可能突出显示“Date Modified”列中具有相同值的行,例如,如果我有两个或多个文件今天被修改,则该行将突出显示为绿色,Python QFileSystemModel QTableView日期修改高亮显示,python,pyqt,qtableview,qfilesystemmodel,Python,Pyqt,Qtableview,Qfilesystemmodel,我正在尝试使用QFileSystemModel和QTableView制作一个小文件浏览器 我想知道是否有可能突出显示“Date Modified”列中具有相同值的行,例如,如果我有两个或多个文件今天被修改,则该行将突出显示为绿色, 昨天修改过的颜色以绿色突出显示,但阴影较浅,等等。要更改背景色,有几个选项,例如: 重写模型的data()方法,以便与角色Qt.BackgroundRole关联的返回值 使用QIdentityProxyModel修改与Qt.BackgroundRole相关的值,类似
昨天修改过的颜色以绿色突出显示,但阴影较浅,等等。要更改背景色,有几个选项,例如:
- 重写模型的
方法,以便与角色data()
关联的返回值Qt.BackgroundRole
- 使用QIdentityProxyModel修改与
相关的值,类似于前面的选项Qt.BackgroundRole
- 使用
修改QStyledItemDelegate
的QStyleOptionViewItem
属性backgroundBrush
from PyQt5 import QtCore, QtGui, QtWidgets
class DateDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
model = index.model()
if isinstance(model, QtWidgets.QFileSystemModel):
dt = model.lastModified(index)
today = QtCore.QDateTime.currentDateTime()
yesterday = today.addDays(-1)
if dt < yesterday:
option.backgroundBrush = QtGui.QColor(0, 255, 0)
else:
option.backgroundBrush = QtGui.QColor(0, 155, 0)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
path_dir = QtCore.QDir.currentPath()
view = QtWidgets.QTableView()
model = QtWidgets.QFileSystemModel()
view.setModel(model)
model.setRootPath(path_dir)
view.setRootIndex(model.index(path_dir))
view.show()
delegate = DateDelegate(view)
view.setItemDelegate(delegate)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
从PyQt5导入QtCore、QtGui、qtwidget
类DateDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self、option、index):
super().initStyleOption(选项,索引)
model=index.model()
如果isinstance(模型,qtwidts.QFileSystemModel):
dt=模型。上次修改(索引)
今天=QtCore.QDateTime.currentDateTime()
昨天=今天。添加天数(-1)
如果dt<昨天:
option.backgroundBrush=QtGui.QColor(0,255,0)
其他:
option.backgroundBrush=QtGui.QColor(0,155,0)
def main():
导入系统
app=qtwidts.QApplication(sys.argv)
path_dir=QtCore.QDir.currentPath()
view=QtWidgets.QTableView()
model=qtwidts.QFileSystemModel()
view.setModel(model)
model.setRootPath(路径\目录)
view.setRootIndex(model.index(path_dir))
view.show()
delegate=DateDelegate(视图)
view.setItemDelegate(委托)
sys.exit(app.exec_())
如果名称=“\uuuuu main\uuuuuuuu”:
main()