带有QFileSystemModel的Python Qt QTreeView想要为其创建一个搜索字段

2024-05-16 11:07:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和Qt接口开发有点陌生。 为了说明这一点,我正在为maya编写一个管道接口,并希望为QTreeView创建一个搜索字段

我的treeview还可以,我创建了搜索字段,但这个字段并不精确,在我启动搜索时也找不到所有内容

我给你一段代码,希望能帮助你理解我的问题

def searchAndDropWidget(self):
    """Creating the search and drop widget.
    We can use it to search folders and files in the path of the tree structure and drag/drop files in the software.
    """

    self.hLine = QHLineShape()
    self.hLine.setFixedWidth(474)
    self.hLine.setMinimumHeight(7)
    # Create the searchNDrop widget.
    self.searchNDropWidget = QtWidgets.QWidget()

    # Create the searchNDrop Layout.
    self.searchNDropLayout = QtWidgets.QVBoxLayout(self.childWidget1)
    self.searchNDropLayout.setContentsMargins(10, 0, 0, 0)
    # Create and set the search label.
    self.searchLabel = QtWidgets.QLabel('Search :')
    self.searchLabel.setFont(QtGui.QFont('Times', 10))
    # Create the search line editor.
    self.searchLineEdit = QtWidgets.QLineEdit()
    # Create the QtreeView searcher.
    self.searchTreeView = QtWidgets.QTreeView()
    
    self.searchNDropInteract()
    
    # Add the widgets to the searchNDropLayout.
    self.searchNDropLayout.addWidget(self.hLine)
    self.searchNDropLayout.addWidget(self.searchLabel, 0, QtCore.Qt.AlignHCenter)
    self.searchNDropLayout.addWidget(self.searchLineEdit)
    self.searchNDropLayout.addWidget(self.searchTreeView)

    self.text = self.searchLineEdit.text()

    # Add the layout to the widget.
    self.searchNDropWidget.setLayout(self.searchNDropLayout)

    self.searchLineEdit.returnPressed.connect(self.onTextChanged)

def searchNDropInteract(self):
    """Here is the search and drop interaction code.
    We use condition to set the path and the QTreeview point on this path.
    After we define the model of the QTreeview and we use Filters and Sort filter proxy model
    to search in the tree structure.
    """

    # Get the current index of the team.
    self.teamIdx = self.comboBoxTeam.currentIndex()

    # Condition to determine the server or local path.
    if(self.localCheckBox.isChecked() == True):
        self.path = self.localPath
    else:          
        if(self.teamIdx == 0):
            self.path = "P:\shows\IA"
        elif(self.teamIdx == 1):
            self.path = "P:\shows\LDS"
        elif(self.teamIdx == 2):
            self.path = "P:\shows\OPS"

    # Define the file system model.
    self.model = QtWidgets.QFileSystemModel(self.searchTreeView)

    # Set the root path.
    self.model.setRootPath(self.path)

    self.model.setFilter(QtCore.QDir.NoDotAndDotDot
        | QtCore.QDir.AllEntries
        | QtCore.QDir.Files
        | QtCore.QDir.Dirs)
    
    self.proxyModel = QtCore.QSortFilterProxyModel(recursiveFilteringEnabled = True,
    filterRole=QtWidgets.QFileSystemModel.FileNameRole)

    #QtCore.QCoreApplication.processEvents()

    self.proxyModel.setSourceModel(self.model)

    # Set the model, the root index and the drop possibility.
    self.searchTreeView.setModel(self.proxyModel)
    
    self.adjust_root_index()
    
    self.searchTreeView.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
    self.searchTreeView.setColumnHidden(1, True)
    self.searchTreeView.setColumnHidden(2, True)
    self.searchTreeView.setColumnHidden(3, True)
    self.searchTreeView.setAcceptDrops(True)
    self.searchTreeView.setDragDropOverwriteMode(True)
    self.searchTreeView.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
    
    

def onTextChanged(self):
    """This function allows to expand or collapse all the tree structure
    with as condition the fact to write or not in the search line edit.
    """
    

    self.text = self.searchLineEdit.text()
    
    if(self.text != ""):
        self.searchTreeView.expandAll()
        
    else:
        self.searchTreeView.collapseAll()

    self.proxyModel.setFilterWildcard("*{}*".format(self.text))

    self.adjust_root_index()

def adjust_root_index(self):
    
    self.rootIndex = self.model.index(self.path)
    
    self.proxyIndex = self.proxyModel.mapFromSource(self.rootIndex)
    
    self.searchTreeView.setRootIndex(self.proxyIndex)

my Search who don't obtain results but there is files with that in folders


Tags: andthetopathtextinselftrue