1

我希望有可能在QLineEditQTextEditQLineEdit. 这是我使用的一段代码(非常简单):

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如果我添加逗号,我希望完成者再次“开始预测”相同的单词。谢谢。

4

3 回答 3

5

如果它可以帮助其他人,我已经找到了答案,我为 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)
于 2016-03-29T23:01:19.417 回答
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_())
于 2018-11-26T20:12:02.920 回答
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_())
于 2020-03-12T13:21:38.243 回答