zu meinem Code, habe ich Fragen.
Ich möchte
Wenn ich in QLineEdit eine virtuelle Tasteneingabe z.B. "self.setText(os.sep)" mache, werden nicht die Pfade von / root im Completer ausgegeben.
Ich habe schon versucht in Code-Zeile 35 einen Ansatz zu finden, leider ....
Gibt es eine Möglichkeit, dies umzusetzen?
Grüße Nobuddy
Code: Alles auswählen
import os
import sys
from PyQt5.QtCore import (
Qt,
QDir
)
from PyQt5.QtWidgets import (
QApplication,
QLineEdit,
QCompleter,
QFileSystemModel
)
class FileSystemCompleter(QCompleter):
def __init__(self, parent=None):
super(FileSystemCompleter, self).__init__(parent=parent)
self.parent = parent
# settings
self.setCaseSensitivity(Qt.CaseInsensitive)
self.setFilterMode(Qt.MatchStartsWith)
self.setCompletionRole(QFileSystemModel.FileNameRole)
# model
self.model_ = QFileSystemModel(self)
self.model_.setFilter(
QDir.NoDotAndDotDot | QDir.AllDirs | QDir.Files
)
self.model_.setRootPath(QDir.rootPath())
self.setModel(self.model_)
def pathFromIndex(self, index):
path = super().pathFromIndex(index)
if os.path.isdir(path):
# set os.sep on end of input field string
#path = f'{path}{os.sep}'
self.model_.setData(index, path, Qt.UserRole)
self.parent.path = path
# get the selected completion suggestion
return os.sep.join([path])
def splitPath(self, path):
# output folders and files from text input
# start parameter
dirPath = startPath = path
# check path
self.parent.path = path
if os.path.isdir(path):
"""it's a directory"""
elif os.path.isfile(path):
"""it's a file"""
else:
"""create directory path and check path"""
self.parent.path = f'{os.sep.join(path.split(os.sep)[:-1])}'
# get path from directory
i = 0
if not startPath.endswith(os.sep):
for i in range(len(startPath)-1, 0, -1):
if startPath[i] == os.sep:
break
dirPath = dirPath[:i]
# get difference from prom path to dirPath
pathValue = ''.join(path.split(dirPath))
if not pathValue.startswith(os.sep):
pathValue = f'{os.sep}{pathValue}'
pathValue = pathValue.replace(os.sep, '')
# check paths to startswith pathValue
check = False
for p in os.listdir(dirPath):
if p.startswith(pathValue):
path = startPath
self.widget().setText(path)
sep = (os.sep if not dirPath.endswith(os.sep)
and not p.startswith(os.sep) else '')
check = True
break
if not check:
path = startPath[:-1]
self.widget().setText(path)
self.widget().setFocus()
self.widget().setCursorPosition(len(self.widget().text())+1)
return super(FileSystemCompleter, self).splitPath(path)
def main():
App = QApplication(sys.argv)
class start(QLineEdit):
def __init__(self):
super().__init__()
self.textChanged[str].connect(self.searchFieldCallback)
self.completer = FileSystemCompleter
self.setCompleter(self.completer(parent=self))
#self.searchFieldCallback(os.sep)
def searchFieldCallback(self, text):
#if text != self.text():
# self.setText(text)
# get job on completer
self.completer(parent=self)
try:
# Path selected, ready for further processing
print('self.path: ', self.path)
self.paths = []
if os.path.isdir(self.path):
# get parent current paths
self.paths = sorted(os.listdir(self.path))
print('self.paths: ', self.paths)
del self.path
except AttributeError:
pass
win = start()
win.show()
sys.exit(App.exec())
if __name__ == '__main__':
main()