如何在QLineedit completer中删除光标

时间:2019-06-18 09:43:22

标签: python pyqt pyqt4 qlineedit qcompleter

这是我的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_())

我得到了这样的图像:

enter image description here

1 个答案:

答案 0 :(得分: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_())