第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

filterAcceptsRow() 究竟做了什么?

filterAcceptsRow() 究竟做了什么?

慕森卡 2022-05-19 14:01:02
一個(gè)目錄中有3個(gè)文件夾:f:/root_folder/folder1f:/root_folder/_folder2f:/root_folder/folder3.asset我嘗試自定義 QSortFilterProxyModel.filterAcceptsRow() 僅顯示帶有 .asset 后綴的文件夾。** 我知道我可以使用 QFileSystemModel.setNameFilters([*.asset]) 來做到這一點(diǎn)。但有時(shí)它不起作用。我有 Python3.7 + PySide2 5.13.0。# first inherit a QFileSystemModel instance:listModel = QFileSystemModel()# let the instance display only folders, except for '.' and '..':listModel.setFilter(QDir.NoDotAndDotDot | QDir.Dirs)# assgin a root path. i just want the model to search the 'f:/root_folder':listModel.setRootPath("f:/root_folder")# add a custom QSortFilterProxyModel:myProxy = myProxyModel()myProxy.setSourceModel(listModel)# finally show result in a QListView:# 'ui' is a QWidget object that contain a listView widget.ui.listView.setModel(myProxy)ui.listView.setRootIndex(myProxy.mapFromSource(listModel.index("f:/root_folder")))這是自定義的 QSortFilterProxyModel:# test:class myProxyModel(QSortFilterProxyModel):    def filterAcceptsRow(self, source_row, source_parent):        return True此時(shí),腳本按預(yù)期工作:列表中有 3 個(gè)文件夾且沒有過濾器。如果我理解正確,“source_parent”應(yīng)該是“l(fā)istModel”的 QModelIndex,它指向目錄“f:/root_folder”。并且“source_row”應(yīng)該是“f:/root_folder”(三個(gè)文件夾之一)中某個(gè)項(xiàng)目的“序號(hào)”。對?然后我添加了自己的過濾器:# first try:class myProxyModel(QSortFilterProxyModel):    def filterAcceptsRow(self, source_row, source_parent):        source_model = self.sourceModel()         # 'source_model' should be the 'listModel', right?        source_index = source_model.index(source_row, 0, source_parent)        # 'source_index' is a QModelIndex, pointing to 'folder1' or '_folder2' or 'folder3.asset'.        # start filtering        filename = source_index.data(Qt.DisplayRole)        print(filename) # check        if filename[-6:] == ".asset": return True        else: return False它應(yīng)該在控制臺(tái)上顯示 3 個(gè)文件夾名稱,在列表中顯示 1 個(gè)文件夾(folder3.asset)。但我得到了非常奇怪的結(jié)果!這是控制臺(tái)的結(jié)果:**它多次列出了我所有的硬盤HDD (F:)root_folderHDD (F:)HDD (E:)HDD (D:)C:HDD (F:)HDD (E:)HDD (D:)C:現(xiàn)在我完全糊涂了。filterAcceptsRow() 到底是做什么的?
查看完整描述

2 回答

?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

您只需過濾與 QFileSystemModel 的 rootPath() 關(guān)聯(lián)的 inde 的子項(xiàng):


from PySide2 import QtCore, QtGui, QtWidgets



class SuffixDirProxyModel(QtCore.QSortFilterProxyModel):

    def __init__(self, parent=None):

        super().__init__(parent)

        self._suffix = ""


    def filterAcceptsRow(self, source_row, source_parent):

        source_model = self.sourceModel()

        if (

            self._suffix

            and isinstance(source_model, QtWidgets.QFileSystemModel)

            and source_parent == source_model.index(source_model.rootPath())

        ):

            index = source_model.index(source_row, 0, source_parent)

            name = index.data(QtWidgets.QFileSystemModel.FileNameRole)

            file_info = source_model.fileInfo(index)

            return name.split(".")[-1] == self._suffix and file_info.isDir()

        return True


    @property

    def suffix(self):

        return self._suffix


    @suffix.setter

    def suffix(self, s):

        self._suffix = s

        self.invalidateFilter()



if __name__ == "__main__":

    import sys


    app = QtWidgets.QApplication(sys.argv)


    model = QtWidgets.QFileSystemModel()

    model.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs)


    path = # "f:/root_folder"

    model.setRootPath(path)


    proxy = SuffixDirProxyModel()

    proxy.suffix = "asset"

    proxy.setSourceModel(model)


    w = QtWidgets.QListView()

    w.setViewMode(QtWidgets.QListView.IconMode)

    w.setModel(proxy)

    w.setRootIndex(proxy.mapFromSource(model.index(path)))

    w.show()

    sys.exit(app.exec_())


查看完整回答
反對 回復(fù) 2022-05-19
?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果你不超載它什么都沒有。

QSortFilterProxyModel如果您使用的類中提供的過濾器通常不能滿足您的需求,您只需要重載它。


查看完整回答
反對 回復(fù) 2022-05-19
  • 2 回答
  • 0 關(guān)注
  • 330 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)