PyQt QLineEdit 与 readline 完成器?

标签 pyqt readline qlineedit qcompleter

我一直在开发一个命令行工具,现在我正在为其制作一个 PyQT GUI。我想使用 readline 模块获取当前的自动完成实现,并将其放入 QLineEdit 文本框中。这可能吗?你有什么建议吗?

这是我用 readline 模块做的事情的一个例子:

import readline

values = ['these','are','my','autocomplete','words']
completions = {}

def completer(text,state):
    try:
        matches = completions[text]
    except KeyError:
        matches = [value for value in values if text.upper() in value.upper()]
        completions[text] = matches
    try:
        return matches[state]
    except IndexError:
        return None

readline.set_completer(completer)
readline.parse_and_bind('tab: menu-complete')

whie 1:
    text = raw_input('> ')
    text.dostuff()

最终,如果我不能让 readline 模块在 QLineEdit 小部件中工作,我最终想做的是在一个单词列表上完成,能够用 +- 等符号分隔多个单词*/() 等...

谢谢!

最佳答案

我可以告诉你,首先,尝试围绕新功能包装 QCompleter 是一件非常痛苦的事情。您必须能够满足 QCompleter 的所有接口(interface),并围绕该实际代码桥接它。

您必须手动更新 QCompleter 上的 QStringListModel 集,并为给定的搜索前缀提供获取当前完成和完成总数的实现。

这是一个与 PopupCompletion 模式兼容的工作示例:

import re

class ReadlineCompleter(QtGui.QCompleter):

    def __init__(self, completeFn, *args, **kwargs):
        super(ReadlineCompleter, self).__init__(*args, **kwargs)
        self._completer = completeFn
        self.setModel(QtGui.QStringListModel())
        self.update()

    def setCompletionPrefix(self, val):
        super(ReadlineCompleter, self).setCompletionPrefix(val)
        self.update()

    def currentCompletion(self):
        state = self.currentRow()
        return self._completionAt(state)

    def completionCount(self):
        state = 0
        while True:
            result = self._completionAt(state)
            if not result:
                break
            state += 1
        return state

    def update(self):
        matches = [self._completionAt(i) for i in xrange(self.completionCount())]
        self.model().setStringList(matches)

    def _completionAt(self, state):
        text = str(self.completionPrefix())

        # regex to split on any whitespace, or the char set +*/^()-
        match = re.match(r'^(.*)([\s+*/^()-]+)(.*)$', text)
        if match:
            prefix, sep, text = match.groups()

        result = self._completer(str(text), state)

        if result and match:
            result = sep.join([prefix, result])

        return '' if result is None else result     

请注意,在 _completionAt() 方法中,我添加了您想要的额外功能,用于检测分隔符模式。你可以很明显地调整这个。但它会拆分最后一部分并使用该值来检查是否完成,然后再次使用前缀重新加入结果。

用法

重要。您需要将 QLineEdit 的 textChanged 信号连接到完成器以强制更新。否则,将不会在完成器中使用任何功能。

line = QtGui.QLineEdit()
comp = ReadlineCompleter(completer)
comp.setCompletionMode(comp.PopupCompletion)
line.setCompleter(comp)
# important
line.textChanged.connect(comp.setCompletionPrefix)

examples here显示其他人如何必须在自定义行编辑中填写功能,他们完全绕过完成者的标准信号并自己触发。你可以看到它的一点点努力。

关于PyQt QLineEdit 与 readline 完成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11401367/

相关文章:

python - PyQt5中QTextBrowser中的回车

python - 如何在 PyQt4/PySide 的 eventfilter 中的 QListWidget 之上进行绘制?

python - 在拖放中保留 QStandardItem 子类

go - “附加”的工作方式是什么?

bash - 如何在 Linux Bash Shell 命令行中删除右侧的整个单词

c++ - inputMask 用空格填充 QLineEdit

python - 如何使用 QComboBox 作为 QTableView 的委托(delegate)

c# - 是否有类似于 GNU readline 的 .Net 库?

python - 如何在 QLineedit 完成器中删除光标

c++ - 当我尝试将一个信号连接到插槽时,Qt 代码无法编译