1

我已经阅读了 QCompleter 文档(https://doc.qt.io/qt-5/qcompleter.html),并且尝试为 QPlainTextEdit 实现 QCompleter。

现在我让它像这样工作:

在此处输入图像描述

但问题是,如果你开始写一个在keyword.kwlist创建的列表中的单词,那么它会专注于光标下弹出的弹出窗口,它不会让我继续输入。

但是在 QCompleter 文档页面上将代码从 c++ 转换为 python 时,即使它在下面为我提供了一些单词选择,我仍然可以输入。

我尝试将焦点设置为 self.editor 但这没有用。我需要这方面的帮助以及弹出窗口的位置。现在它有点挡住了这个词的视野。

它的功能应该是这样的:

在此处输入图像描述

但这仅适用于 QLineEdit。

from PyQt5.QtWidgets import QCompleter, QPlainTextEdit, QApplication, QWidget, QHBoxLayout
import sys
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QTextCursor, QFont, QTextOption
import keyword

class Completer(QCompleter):

    insertText = pyqtSignal(str)

    def __init__(self, myKeywords=None, parent=None):

        myKeywords = keyword.kwlist

        QCompleter.__init__(self, myKeywords, parent)
        self.activated.connect(self.changeCompletion)

    def changeCompletion(self, completion):
        if completion.find("(") != -1:
            completion = completion[:completion.find("(")]
            print(completion)
        print("completion is " + str(completion))
        self.insertText.emit(completion + " ")
        self.popup().hide()


class MyTextEdit(QWidget):

    def __init__(self, *args):
        super().__init__(*args)
        font = QFont()

        font.setPointSize(12)
        self.editor = QPlainTextEdit()
        self.setFont(font)
        self.completer = None
        self.hbox = QHBoxLayout(self)
        self.editor.textChanged.connect(self.complete)
        self.hbox.addWidget(self.editor)

    def setCompleter(self, completer):
        if self.completer:
            print("completer is: " + str(completer))
            self.disconnect()

        if not completer:
            print("completer is: " + str(completer))
            return

        completer.setWidget(self)
        completer.setCompletionMode(QCompleter.PopupCompletion)
        completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer = completer

        self.completer.insertText.connect(self.insertCompletion)

    def insertCompletion(self, completion):
        tc = self.editor.textCursor()
        extra = (len(completion) - len(self.completer.completionPrefix()))
        tc.movePosition(QTextCursor.Left)
        tc.movePosition(QTextCursor.EndOfWord)
        tc.insertText(completion[-extra:])
        self.editor.setTextCursor(tc)

    def textUnderCursor(self):
        tc = self.editor.textCursor()
        tc.select(QTextCursor.WordUnderCursor)
        return tc.selectedText()

    def complete(self):
        completionPrefix = self.textUnderCursor()
        print("completion prefix is: " + str(completionPrefix))

        self.completer.setCompletionPrefix(completionPrefix)
        popup = self.completer.popup()
        popup.setCurrentIndex(
            self.completer.completionModel().index(0, 0))
        cr = self.editor.cursorRect()
        cr.setWidth(
            self.completer.popup().sizeHintForColumn(0) + self.completer.popup().verticalScrollBar().sizeHint().width())

        self.completer.complete(cr)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    completer = Completer()
    te = MyTextEdit()
    te.setCompleter(completer)
    te.show()
    sys.exit(app.exec_())
4

0 回答 0