我希望有可能在我的QLineEdit
中多次使用自动完成功能,我使用QTextEdit
找到了示例,但我无法找到QLineEdit
。这是我使用的一段代码(非常简单):
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
def main():
app = QApplication(sys.argv)
edit = QLineEdit()
strList = ["Germany", "Spain", "France", "Norway"]
completer = QCompleter(strList,edit)
edit.setCompleter(completer)
edit.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
例如,我希望完成者能够开始预测"如果我添加一个逗号,同样QLineEdit
中的单词。
感谢。
答案 0 :(得分:2)
如果它可以帮助其他人,我找到了答案,我为Completer创建了一个类:
class Completer(QtWidgets.QCompleter):
def __init__(self, parent=None):
super(Completer, self).__init__(parent)
self.setCaseSensitivity(Qt.CaseInsensitive)
self.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
self.setWrapAround(False)
# Add texts instead of replace
def pathFromIndex(self, index):
path = QtWidgets.QCompleter.pathFromIndex(self, index)
lst = str(self.widget().text()).split(',')
if len(lst) > 1:
path = '%s, %s' % (','.join(lst[:-1]), path)
return path
# Add operator to separate between texts
def splitPath(self, path):
path = str(path.split(',')[-1]).lstrip(' ')
return [path]
我在QLineEdit的类中使用它,如:
class TextEdit(QtWidgets.QLineEdit):
def __init__(self, parent=None):
super(TextEdit, self).__init__(parent)
self.setPlaceholderText("example : ")
self._completer = Completer(self)
self.setCompleter(self._completer)
答案 1 :(得分:1)
PyQt4解决方案:
from PyQt4 import QtCore, QtGui
class Completer(QtGui.QCompleter):
def __init__(self, *args, **kwargs):
super(Completer, self).__init__(*args, **kwargs)
self.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.setCompletionMode(QtGui.QCompleter.PopupCompletion)
self.setWrapAround(False)
# Add texts instead of replace
def pathFromIndex(self, index):
path = QtGui.QCompleter.pathFromIndex(self, index)
lst = str(self.widget().text()).split(',')
if len(lst) > 1:
path = '%s, %s' % (','.join(lst[:-1]), path)
return path
def splitPath(self, path):
path = str(path.split(',')[-1]).lstrip(' ')
return [path]
class TextEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(TextEdit, self).__init__(parent)
words = ["alpha", "omega", "omicron", "zeta"]
self.setPlaceholderText("example : ")
self._completer = Completer(words, self)
self.setCompleter(self._completer)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = TextEdit()
w.show()
sys.exit(app.exec_())
答案 2 :(得分:0)
以下是我发现对某人可能有用的内容:
class LineEdit(QLineEdit):
def __init__(self, *args, **kwargs):
super(LineEdit,self).__init__( *args, **kwargs)
self.multipleCompleter = None
def keyPressEvent(self, event):
QLineEdit.keyPressEvent(self, event)
if not self.multipleCompleter:
return
c = self.multipleCompleter
if self.text() == "":
return
c.setCompletionPrefix(self.cursorWord(self.text()))
if len(c.completionPrefix()) < 1:
c.popup().hide()
return
c.complete()
def cursorWord(self, sentence):
p = sentence.rfind(" ")
if p == -1:
return sentence
return sentence[p + 1:]
def insertCompletion(self, text):
p = self.text().rfind(" ")
if p == -1:
self.setText(text)
else:
self.setText(self.text()[:p+1]+ text)
def setMultipleCompleter(self, completer):
self.multipleCompleter = completer
self.multipleCompleter.setWidget(self)
completer.activated.connect(self.insertCompletion)
def main():
app = QApplication(sys.argv)
w = LineEdit()
completer = QCompleter(["Animals", "Dogs", "Birds", "Cats", "Elephant", "Zebra"])
completer.setCaseSensitivity(Qt.CaseInsensitive)
w.setMultipleCompleter(completer)
w.show()
sys.exit(app.exec_())