QFileSystemModel QTableView Date Modified highlighting

て烟熏妆下的殇ゞ 提交于 2021-02-16 20:54:06

问题


I am trying to make a little file browser using QFileSystemModel and QTableView.

I was wondering if it is possible to highlight rows with same value in "Date Modified" column, for instance if I have two or more files which been modified today row gets highlighted in green, those modified yesterday highlighted in green but lighter shade, etc.


回答1:


To change the background color there are several options such as:

  • override the data() method of the model so that the return value associated with the role Qt.BackgroundRole.

  • Use a QIdentityProxyModel that modifies the value associated with Qt.BackgroundRole similar to the previous option

  • Use a QStyledItemDelegate to modify the backgroundBrush property of QStyleOptionViewItem.

The simplest option is the last option so I will show your implementation:

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()


来源:https://stackoverflow.com/questions/62377343/qfilesystemmodel-qtableview-date-modified-highlighting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!