2

这是我的 Qlineedit Completer 示例程序。在自动建议之后,我想在我写的行编辑中将文本显示为项目的开始completer.activated.connect(lambda: QTimer.singleShot(0, lambda: edit.home(False)))。它工作正常,但我不想在乞讨时显示光标,并在自动建议后显示光标。

下面是我的代码:

import sys
from PyQt4.QtCore import Qt,QTimer
from PyQt4.QtGui import QApplication, QCompleter, QLineEdit, QStringListModel

def get_data(model):
   model.setStringList(["completionProgramxxxxxxxxxxxxxxx", "completionProgramyyyyyy","truthordisabled", "storecredit", "iamhere"])

if __name__ == "__main__":

    app = QApplication(sys.argv)
    edit = QLineEdit()
    completer = QCompleter()
    edit.setCompleter(completer)
    completer.activated.connect(lambda:  QTimer.singleShot(0, lambda: edit.home(False)))

    model = QStringListModel()
    completer.setModel(model)
    get_data(model)
    edit.show()
    sys.exit(app.exec_())

我得到了这样的图像:

在此处输入图像描述

4

1 回答 1

2

你必须清理焦点:

import sys
from PyQt4 import QtCore, QtGui


def get_data(model):
    model.setStringList(
        [
            "completionProgramxxxxxxxxxxxxxxx",
            "completionProgramyyyyyy",
            "truthordisabled",
            "storecredit",
            "iamhere",
        ]
    )


class Completer(QtGui.QCompleter):
    def __init__(self, parent=None):
        super(Completer, self).__init__(parent)
        self.activated.connect(self.on_activated)

    @QtCore.pyqtSlot()
    def on_activated(self):
        le = self.widget()
        if isinstance(le, QtGui.QLineEdit):
            le.home(False)
            le.clearFocus()


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    edit = QtGui.QLineEdit()
    completer = Completer(edit)
    edit.setCompleter(completer)
    model = QtGui.QStringListModel(edit)
    completer.setModel(model)
    get_data(model)
    edit.show()
    sys.exit(app.exec_())
于 2019-06-18T10:06:31.877 回答