I try to build this example in python for a little project im working on: Qt QListWidget With Filter . But I've trouble understanding it. I create a list of all files from my folder and want to filter it with text I write in the lineedit, like a real time search.
import GUI_update_v8
from PyQt5 import QtGui, QtCore, QtWidgets
import sys
import os
class MyFileBrowser(GUI_update_v8.Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self):
super(MyFileBrowser,self).__init__()
self.setupUi(self)
self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.treeView.customContextMenuRequested.connect(self.context_menu)
self.listWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.listWidget.customContextMenuRequested.connect(self.context_menu1)
self.populate()
def populate(self):
"""Verzeichnisübersicht Rechte Seite"""
global path
path =("T:\\Documentation")
self.model = QtWidgets.QFileSystemModel()
self.model.setRootPath((QtCore.QDir.rootPath()))
global proxymodel
proxymodel = QtCore.QSortFilterProxyModel(recursiveFilteringEnabled=True)
proxymodel.setSourceModel(self.model)
proxymodel.sort(0, QtCore.Qt.AscendingOrder)
self.treeView.setModel(proxymodel)
self.treeView.setRootIndex(proxymodel.mapFromSource(self.model.index(path)))
self.treeView.setColumnWidth(0,350)
x = [i[2] for i in os.walk('T:\\Documentation')]
y=[]
for t in x:
for f in t:
y.append(f)
self.listWidget.addItems(y)
self.listWidget.sortItems()
self.lineEdit.setPlaceholderText("Search here")
def Change():
s = QtWidgets.QLineEdit()
suche = s.text()
e = QtCore.QRegularExpression(suche)
q = self.listWidget.findItems(filter(e))
self.listWidget.clear()
self.listWidget.addItems(filterlist)
self.lineEdit.textChanged.connect(Change)
if __name__ =="__main__":
app = QtWidgets.QApplication([])
fb = MyFileBrowser()
fb.show()
app.exec_()
I get as an error: filter expects 2 arguments, got 1
.
When you try to translate examples from one language to another, do not be a copy and paste machine, but you must analyze the code since not all functions will necessarily exist or have the same name. The logic of the video is to create a base list containing all the options, and then filter based on the regular expressions
def populate(self):
path = 'T:\\Documentation'
self.model = QtWidgets.QFileSystemModel()
self.model.setRootPath((QtCore.QDir.rootPath()))
proxymodel = QtCore.QSortFilterProxyModel(recursiveFilteringEnabled=True)
proxymodel.setSourceModel(self.model)
proxymodel.sort(0, QtCore.Qt.AscendingOrder)
self.treeView.setModel(proxymodel)
self.treeView.setRootIndex(proxymodel.mapFromSource(self.model.index(path)))
self.treeView.setColumnWidth(0, 350)
self.files = []
for _, _, files in os.walk(path):
self.files.extend(files)
self.listWidget.addItems(self.files)
self.listWidget.sortItems()
self.lineEdit.setPlaceholderText("Search here")
self.lineEdit.textChanged.connect(self.change)
def change(self, text):
self.listWidget.clear()
e = QtCore.QRegularExpression(text)
filter_files = [f for f in self.files if e.match(f).hasMatch()]
self.listWidget.addItems(filter_files)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.