pyqt5 qtextedit auto completion

  • Last Update :
  • Techknowledgy :

mMyDictionaryCompleter.py

# === === === === === === === === === === === === === === === === === === === === === === === === === === =
   # MyDictionaryCompleter
# === === === === === === === === === === === === === === === === === === === === === === === === === === =
   from PyQt4
import QtGui, QtCore
class MyDictionaryCompleter(QtGui.QCompleter):
   # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # class Variables
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   insertText = QtCore.pyqtSignal(str)
#no classVariables
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # Constructor
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   def __init__(self, myKeywords = None, parent = None):

   myKeywords = ['apple', 'aggresive', 'ball', 'bat', 'cat', 'cycle', 'dog', 'dumb', \
      'elephant', 'engineer', 'food', 'file', 'good', 'great', \
      'hippopotamus', 'hyper', 'india', 'ireland', 'just', 'just', \
      'key', 'kid', 'lemon', 'lead', 'mute', 'magic', \
      'news', 'newyork', 'orange', 'oval', 'parrot', 'patriot', \
      'question', 'queue', 'right', 'rest', 'smile', 'simple', \
      'tree', 'urban', 'very', 'wood', 'xylophone', 'yellow', \
      'zebra'
   ]
QtGui.QCompleter.__init__(self, myKeywords, parent)
self.connect(self,
   QtCore.SIGNAL("activated(const QString&)"), self.changeCompletion)
# | -- -- -- -- -- -- -- -- -- -- -- -- --End of Constructor-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # changeCompletion
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   def changeCompletion(self, completion):
   if completion.find("(") != -1:
   completion = completion[: completion.find("(")]
print(completion)
self.insertText.emit(completion)
# | -- -- -- -- -- -- -- -- -- -- -- - End of changeCompletion-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |

Editor

from PyQt5.QtWidgets
import QCompleter, QPlainTextEdit
from PyQt5.QtCore
import Qt
from PyQt5.QtGui
import QTextCursor
import MyCompleter

class AwesomeTextEdit(QPlainTextEdit):
   def __init__(self, parent = None):
   super(AwesomeTextEdit, self).__init__(parent)

self.completer = MyCompleterparent()
self.completer.setWidget(self)
self.completer.insertText.connect(self.insertCompletion)

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

def focusInEvent(self, event):
   if self.completer:
   self.completer.setWidget(self)
QPlainTextEdit.focusInEvent(self, event)

def keyPressEvent(self, event):

   tc = self.textCursor()
if event.key() == Qt.Key_Tab and self.completer.popup().isVisible():
   self.completer.insertText.emit(self.completer.getSelected())
self.completer.setCompletionMode(QCompleter.PopupCompletion)
return

QPlainTextEdit.keyPressEvent(self, event)
tc.select(QTextCursor.WordUnderCursor)
cr = self.cursorRect()

if len(tc.selectedText()) > 0:
   self.completer.setCompletionPrefix(tc.selectedText())
popup = self.completer.popup()
popup.setCurrentIndex(self.completer.completionModel().index(0, 0))

cr.setWidth(self.completer.popup().sizeHintForColumn(0) +
   self.completer.popup().verticalScrollBar().sizeHint().width())
self.completer.complete(cr)
else:
   self.completer.popup().hide()

Completer

from PyQt5.QtWidgets
import QCompleter
from PyQt5
import QtCore

class MyCompleter(QCompleter):
   insertText = QtCore.pyqtSignal(str)

def __init__(self, parent = None):
   QCompleter.__init__(self, ["test", "foo", "bar"], parent)
self.setCompletionMode(QCompleter.PopupCompletion)
self.highlighted.connect(self.setHighlighted)

def setHighlighted(self, text):
   self.lastSelected = text

def getSelected(self):
   return self.lastSelected

Suggestion : 2

This example adds auto complete to a QLineEdit text box.,The QLineEdit widget is a simpe text box that can be added to your window.You can create a line edit widget with the line self.lineedit = QLineEdit(). The line edit otherwise works as normal.,If you forget the last line, the QCompleter and QLineEdit are not connected, meaning there is no auto completion.,You may know this from the web, Google search often shows recommendations while you are typing. You can do a similar thing with PyQt.

12
names = ["Apple", "Alps", "Berry", "Cherry"] completer = QCompleter(names)
1
self.lineedit = QLineEdit()
self.lineedit.setCompleter(completer)
12345678910111213141516171819202122

Suggestion : 3

Looking for a way to have an auto completion anycodings_python-3.x with a QTextEdit and QCompleter. I have read anycodings_python-3.x that it is possible but didn't find any anycodings_python-3.x example... I'm using python3.4 and PyQt5,I'm looking for a very basic example thanks anycodings_python-3.x for any help,shortcut keys are Ctrl+Space to show anycodings_pyqt suggestions and Ctrl+E to autocomplete anycodings_pyqt the first avialable suggestion,If anyone interested here is an anycodings_pyqt "incomplete" solution. Here is what I anycodings_pyqt did. I have change to PlainTextEdit anycodings_pyqt because there was no significant anycodings_pyqt advantages to use QTextEdit

mMyDictionaryCompleter.py

# === === === === === === === === === === === === === === === === === === === === === === === === === === =
   # MyDictionaryCompleter
# === === === === === === === === === === === === === === === === === === === === === === === === === === =
   from PyQt4
import QtGui, QtCore
class MyDictionaryCompleter(QtGui.QCompleter):
   # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # class Variables
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   insertText = QtCore.pyqtSignal(str)
#no classVariables
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # Constructor
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   def __init__(self, myKeywords = None, parent = None):

   myKeywords = ['apple', 'aggresive', 'ball', 'bat', 'cat', 'cycle', 'dog', 'dumb', \
      'elephant', 'engineer', 'food', 'file', 'good', 'great', \
      'hippopotamus', 'hyper', 'india', 'ireland', 'just', 'just', \
      'key', 'kid', 'lemon', 'lead', 'mute', 'magic', \
      'news', 'newyork', 'orange', 'oval', 'parrot', 'patriot', \
      'question', 'queue', 'right', 'rest', 'smile', 'simple', \
      'tree', 'urban', 'very', 'wood', 'xylophone', 'yellow', \
      'zebra'
   ]
QtGui.QCompleter.__init__(self, myKeywords, parent)
self.connect(self,
   QtCore.SIGNAL("activated(const QString&)"), self.changeCompletion)
# | -- -- -- -- -- -- -- -- -- -- -- -- --End of Constructor-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # changeCompletion
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   def changeCompletion(self, completion):
   if completion.find("(") != -1:
   completion = completion[: completion.find("(")]
print(completion)
self.insertText.emit(completion)
# | -- -- -- -- -- -- -- -- -- -- -- - End of changeCompletion-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |

Editor

from PyQt5.QtWidgets
import QCompleter, QPlainTextEdit
from PyQt5.QtCore
import Qt
from PyQt5.QtGui
import QTextCursor
import MyCompleter

class AwesomeTextEdit(QPlainTextEdit):
   def __init__(self, parent = None):
   super(AwesomeTextEdit, self).__init__(parent)

self.completer = MyCompleterparent()
self.completer.setWidget(self)
self.completer.insertText.connect(self.insertCompletion)

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

def focusInEvent(self, event):
   if self.completer:
   self.completer.setWidget(self)
QPlainTextEdit.focusInEvent(self, event)

def keyPressEvent(self, event):

   tc = self.textCursor()
if event.key() == Qt.Key_Tab and self.completer.popup().isVisible():
   self.completer.insertText.emit(self.completer.getSelected())
self.completer.setCompletionMode(QCompleter.PopupCompletion)
return

QPlainTextEdit.keyPressEvent(self, event)
tc.select(QTextCursor.WordUnderCursor)
cr = self.cursorRect()

if len(tc.selectedText()) > 0:
   self.completer.setCompletionPrefix(tc.selectedText())
popup = self.completer.popup()
popup.setCurrentIndex(self.completer.completionModel().index(0, 0))

cr.setWidth(self.completer.popup().sizeHintForColumn(0) +
   self.completer.popup().verticalScrollBar().sizeHint().width())
self.completer.complete(cr)
else:
   self.completer.popup().hide()

Completer

from PyQt5.QtWidgets
import QCompleter
from PyQt5
import QtCore

class MyCompleter(QCompleter):
   insertText = QtCore.pyqtSignal(str)

def __init__(self, parent = None):
   QCompleter.__init__(self, ["test", "foo", "bar"], parent)
self.setCompletionMode(QCompleter.PopupCompletion)
self.highlighted.connect(self.setHighlighted)

def setHighlighted(self, text):
   self.lastSelected = text

def getSelected(self):
   return self.lastSelected

Suggestion : 4

Looking for a way to have an auto completion with a QTextEdit and QCompleter. I have read that it is possible but didn't find any example... I'm using python3.4 and PyQt5, 3 days ago Aug 03, 2010  · QTextEdit with autocompletion using pyqt Problem - I wanted a text-edit which would help enter long words. Solution - I have converted the C++ example code into python, and post it here to for all to see. , Detailed Description You can use QCompleter to provide auto completions in any Qt widget, such as QLineEdit and QComboBox. When the user starts typing a word, QCompleter suggests possible ways of completing the word, based on a word list. The word list is provided as a QAbstractItemModel. , AutoResizingTextEdit is a simple Qt widget based on QTextEdit. Its purpose is to automatically adjust editor height to match the text, with the help of a layout it’s placed in.


# === === === === === === === === === === === === === === === === === === === === === === === === === === = # MyDictionaryCompleter # === === === === === === === === === === === === === === === === === === === === === === === === === === = from PyQt4
import QtGui, QtCore class MyDictionaryCompleter(QtGui.QCompleter): # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # class Variables # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | insertText = QtCore.pyqtSignal(str) #no classVariables # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # Constructor # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | def __init__(self, myKeywords = None, parent = None): myKeywords = ['apple', 'aggresive', 'ball', 'bat', 'cat', 'cycle', 'dog', 'dumb', \'elephant', 'engineer', 'food', 'file', 'good', 'great', \'hippopotamus', 'hyper', 'india', 'ireland', 'just', 'just', \'key', 'kid', 'lemon', 'lead', 'mute', 'magic', \'news', 'newyork', 'orange', 'oval', 'parrot', 'patriot', \'question', 'queue', 'right', 'rest', 'smile', 'simple', \'tree', 'urban', 'very', 'wood', 'xylophone', 'yellow', \'zebra'] QtGui.QCompleter.__init__(self, myKeywords, parent) self.connect(self, QtCore.SIGNAL("activated(const QString&)"), self.changeCompletion) # | -- -- -- -- -- -- -- -- -- -- -- -- --End of Constructor-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # changeCompletion # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | def changeCompletion(self, completion): if completion.find("(") != -1: completion = completion[: completion.find("(")] print(completion) self.insertText.emit(completion) # | -- -- -- -- -- -- -- -- -- -- -- - End of changeCompletion-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
# === === === === === === === === === === === === === === === === === === === === === === === === === === = # MyDictionaryCompleter # === === === === === === === === === === === === === === === === === === === === === === === === === === = from PyQt4
import QtGui, QtCore class MyDictionaryCompleter(QtGui.QCompleter): # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # class Variables # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | insertText = QtCore.pyqtSignal(str) #no classVariables # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # Constructor # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | def __init__(self, myKeywords = None, parent = None): myKeywords = ['apple', 'aggresive', 'ball', 'bat', 'cat', 'cycle', 'dog', 'dumb', \'elephant', 'engineer', 'food', 'file', 'good', 'great', \'hippopotamus', 'hyper', 'india', 'ireland', 'just', 'just', \'key', 'kid', 'lemon', 'lead', 'mute', 'magic', \'news', 'newyork', 'orange', 'oval', 'parrot', 'patriot', \'question', 'queue', 'right', 'rest', 'smile', 'simple', \'tree', 'urban', 'very', 'wood', 'xylophone', 'yellow', \'zebra'] QtGui.QCompleter.__init__(self, myKeywords, parent) self.connect(self, QtCore.SIGNAL("activated(const QString&)"), self.changeCompletion) # | -- -- -- -- -- -- -- -- -- -- -- -- --End of Constructor-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | # changeCompletion # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | def changeCompletion(self, completion): if completion.find("(") != -1: completion = completion[: completion.find("(")] print(completion) self.insertText.emit(completion) # | -- -- -- -- -- -- -- -- -- -- -- - End of changeCompletion-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
from PyQt5.QtWidgets
import QCompleter, QPlainTextEdit from PyQt5.QtCore
import Qt from PyQt5.QtGui
import QTextCursor
import MyCompleter class AwesomeTextEdit(QPlainTextEdit): def __init__(self, parent = None): super(AwesomeTextEdit, self).__init__(parent) self.completer = MyCompleterparent() self.completer.setWidget(self) self.completer.insertText.connect(self.insertCompletion) def insertCompletion(self, completion): tc = self.textCursor() extra = (len(completion) - len(self.completer.completionPrefix())) tc.movePosition(QTextCursor.Left) tc.movePosition(QTextCursor.EndOfWord) tc.insertText(completion[-extra: ]) self.setTextCursor(tc) self.completer.popup().hide() def focusInEvent(self, event): if self.completer: self.completer.setWidget(self) QPlainTextEdit.focusInEvent(self, event) def keyPressEvent(self, event): tc = self.textCursor() if event.key() == Qt.Key_Tab and self.completer.popup().isVisible(): self.completer.insertText.emit(self.completer.getSelected()) self.completer.setCompletionMode(QCompleter.PopupCompletion) return QPlainTextEdit.keyPressEvent(self, event) tc.select(QTextCursor.WordUnderCursor) cr = self.cursorRect() if len(tc.selectedText()) > 0: self.completer.setCompletionPrefix(tc.selectedText()) popup = self.completer.popup() popup.setCurrentIndex(self.completer.completionModel().index(0, 0)) cr.setWidth(self.completer.popup().sizeHintForColumn(0) + self.completer.popup().verticalScrollBar().sizeHint().width()) self.completer.complete(cr)
else: self.completer.popup().hide()
from PyQt5.QtWidgets
import QCompleter from PyQt5
import QtCore class MyCompleter(QCompleter): insertText = QtCore.pyqtSignal(str) def __init__(self, parent = None): QCompleter.__init__(self, ["test", "foo", "bar"], parent) self.setCompletionMode(QCompleter.PopupCompletion) self.highlighted.connect(self.setHighlighted) def setHighlighted(self, text): self.lastSelected = textdef getSelected(self): return self.lastSelected

Suggestion : 5

Tell me how to make autofill in QTextEdit. In QLineEdit, I managed to do autofill using QComplete, but it doesn't work with QTextEdit. Please tell me what needs to be done, or what" technique " of programming should be used here? Thank you in advance! :) Here is this example I found on pythone which is given on C+ [ https://doc.qt.io/qt-5/qtwidgets-tools-customcompleter-example.html ],I don't understand how to substitute my autofill list in this example, and is it necessary to redefine keyPressEvent? I also noticed that the English character "E" is not entered, I suspect that it is somehow ignored.,Looking at the code with modelFromFile, I see that it is not complicated and it can be adapted to a list, for example:,If you look in modelFromFile, you will see that the general code for working with files is called there, so you can not use customcompleter_rc, but take a local file (for example, from the script folder):

Customcompleter_rc very a large, well, very impressive list in it, and only 4 lines of code

def qInitResources():
   QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name,
      qt_resource_data)
def qCleanupResources():
   QtCore.qUnregisterResourceData(0x01, qt_resource_struct,
      qt_resource_name, qt_resource_data)
qInitResources()

If you look in modelFromFile, you will see that the general code for working with files is called there, so you can not use customcompleter_rc, but take a local file (for example, from the script folder):

...
self.completer.setModel(self.modelFromFile('wordlist.txt'))
   ...

Looking at the code with modelFromFile, I see that it is not complicated and it can be adapted to a list, for example:

def modelFromList(self, words):
   return QStringListModel(words, self.completer)

Well, and call it, for example, like this:

words = ['hello', 'foo', 'bar', 'питон']
   ...
   self.completer.setModel(self.modelFromList(words))

Suggestion : 6

an example here...that i've worked on... although it is in python3.3 and pyqt4. I guess it should not make much of a difference.. you will have to change from PyQt4 to from PyQt5 ,If anyone interested here is an "incomplete" solution. Here is what I did. I have change to PlainTextEdit because there was no significant advantages to use QTextEdit,shortcut keys are Ctrl+Space to show suggestions and Ctrl+E to autocomplete the first avialable suggestion

mMyDictionaryCompleter.py

# === === === === === === === === === === === === === === === === === === === === === === === === === === =
   # MyDictionaryCompleter
# === === === === === === === === === === === === === === === === === === === === === === === === === === =
   from PyQt4
import QtGui, QtCore
class MyDictionaryCompleter(QtGui.QCompleter):
   # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # class Variables
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   insertText = QtCore.pyqtSignal(str)
#no classVariables
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # Constructor
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   def __init__(self, myKeywords = None, parent = None):

   myKeywords = ['apple', 'aggresive', 'ball', 'bat', 'cat', 'cycle', 'dog', 'dumb', \
      'elephant', 'engineer', 'food', 'file', 'good', 'great', \
      'hippopotamus', 'hyper', 'india', 'ireland', 'just', 'just', \
      'key', 'kid', 'lemon', 'lead', 'mute', 'magic', \
      'news', 'newyork', 'orange', 'oval', 'parrot', 'patriot', \
      'question', 'queue', 'right', 'rest', 'smile', 'simple', \
      'tree', 'urban', 'very', 'wood', 'xylophone', 'yellow', \
      'zebra'
   ]
QtGui.QCompleter.__init__(self, myKeywords, parent)
self.connect(self,
   QtCore.SIGNAL("activated(const QString&)"), self.changeCompletion)
# | -- -- -- -- -- -- -- -- -- -- -- -- --End of Constructor-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   # changeCompletion
# | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
   def changeCompletion(self, completion):
   if completion.find("(") != -1:
   completion = completion[: completion.find("(")]
print(completion)
self.insertText.emit(completion)
# | -- -- -- -- -- -- -- -- -- -- -- - End of changeCompletion-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |

Editor

from PyQt5.QtWidgets
import QCompleter, QPlainTextEdit
from PyQt5.QtCore
import Qt
from PyQt5.QtGui
import QTextCursor
import MyCompleter

class AwesomeTextEdit(QPlainTextEdit):
   def __init__(self, parent = None):
   super(AwesomeTextEdit, self).__init__(parent)

self.completer = MyCompleterparent()
self.completer.setWidget(self)
self.completer.insertText.connect(self.insertCompletion)

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

def focusInEvent(self, event):
   if self.completer:
   self.completer.setWidget(self)
QPlainTextEdit.focusInEvent(self, event)

def keyPressEvent(self, event):

   tc = self.textCursor()
if event.key() == Qt.Key_Tab and self.completer.popup().isVisible():
   self.completer.insertText.emit(self.completer.getSelected())
self.completer.setCompletionMode(QCompleter.PopupCompletion)
return

QPlainTextEdit.keyPressEvent(self, event)
tc.select(QTextCursor.WordUnderCursor)
cr = self.cursorRect()

if len(tc.selectedText()) > 0:
   self.completer.setCompletionPrefix(tc.selectedText())
popup = self.completer.popup()
popup.setCurrentIndex(self.completer.completionModel().index(0, 0))

cr.setWidth(self.completer.popup().sizeHintForColumn(0) +
   self.completer.popup().verticalScrollBar().sizeHint().width())
self.completer.complete(cr)
else:
   self.completer.popup().hide()

Completer

from PyQt5.QtWidgets
import QCompleter
from PyQt5
import QtCore

class MyCompleter(QCompleter):
   insertText = QtCore.pyqtSignal(str)

def __init__(self, parent = None):
   QCompleter.__init__(self, ["test", "foo", "bar"], parent)
self.setCompletionMode(QCompleter.PopupCompletion)
self.highlighted.connect(self.setHighlighted)

def setHighlighted(self, text):
   self.lastSelected = text

def getSelected(self):
   return self.lastSelected

Suggestion : 7

I’m making a similar script like ActiveTypefor maya with PyQt. Got a problem with my completion, it just shows it every two letters. Of course I want it to display always. Anyone had the same problem? Any help welcome! Simple run version:,You can download a first version of the script here http://josbalcaen.com/scripts/maya/gotyping/,Doesnt a combobox only autofill for one word. What if you have multiple strings and need autocomplete?,Thank you for the suggestion but, I need multiple words and multiple lines, so can’t use combobox. You can type in python code and user defined keywords with commands…

I’m making a similar script like ActiveTypefor maya with PyQt.
Got a problem with my completion, it just shows it every two letters. Of course I want it to display always.
Anyone had the same problem? Any help welcome!
Simple run version:

""
"
Execute and type "testing"
""
"

import sip
from PyQt4
import QtGui, QtCore
import maya.OpenMayaUI as apiUI

def getMayaWindow():
   ptr = apiUI.MQtUtil.mainWindow()
ptr = long(ptr) #Ensure type
return sip.wrapinstance(long(ptr), QtCore.QObject)

class DictionaryCompleter(QtGui.QCompleter):
   def __init__(self, parent = None):
   QtGui.QCompleter.__init__(self, ["testing1", "testing2", "testing3"], parent)

class CompletionTextEdit(QtGui.QTextEdit):
   def __init__(self, parent = None):
   super(CompletionTextEdit, self).__init__(parent)
self.completer = None
self.resize(300, 25)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

def setCompleter(self, completer):
   if not completer: return

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

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

def keyPressEvent(self, event):
   if event.key() == QtCore.Qt.Key_Return:
   self.close()
return

QtGui.QTextEdit.keyPressEvent(self, event)

completionPrefix = self.textUnderCursor()

if (completionPrefix != self.completer.completionPrefix()):
   self.completer.setCompletionPrefix(completionPrefix)
popup = self.completer.popup()
popup.setCurrentIndex(
   self.completer.completionModel().index(0, 0))

cr = self.cursorRect()
cr.setWidth(self.completer.popup().sizeHintForColumn(0) +
   self.completer.popup().verticalScrollBar().sizeHint().width())
self.completer.complete(cr) # # popup it up!

   def mousePressEvent(self, event):
   self.close()

class HotboxWidget(QtGui.QDialog):
   def __init__(self, parent = getMayaWindow()):
   super(HotboxWidget, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.Popup | QtCore.Qt.FramelessWindowHint)

win = HotboxWidget()
completer = DictionaryCompleter()
te = CompletionTextEdit()
te.setCompleter(completer)
te.show()

like substring matches?

completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)

Suggestion : 8

The constructor for TextEdit constructs a TextEdit with a parent and initializes c. The instructions to use the completer is displayed on the TextEdit object, using the setPlainText() function.,The TextEdit class is a subclass of QTextEdit with a custom insertCompletion() slot and it reimplements the keyPressEvent() and the focusInEvent() functions. TextEdit also contains a private function textUnderCursor() and a private instance of QCompleter, c.,The TextEdit class reimplements focusInEvent() function, which is an event handler used to receive keyboard focus events for the widget.,The constructor constructs a MainWindow with a parent and initializes the completer. It also instantiates a TextEdit and sets its completer. A QStringListModel, obtained from modelFromFile(), is used to populate the completer. The MainWindow's central widget is set to TextEdit and its size is set to 500 x 300.

<!DOCTYPE RCC>
<RCC version="1.0">
   <qresource prefix="/">
      <file>resources/wordlist.txt</file>
   </qresource>
</RCC>
class TextEdit: public QTextEdit {
   Q_OBJECT

   public:
      TextEdit(QWidget * parent = nullptr);
   ~TextEdit();

   void setCompleter(QCompleter * c);
   QCompleter * completer() const;

   protected:
      void keyPressEvent(QKeyEvent * e) override;
   void focusInEvent(QFocusEvent * e) override;

   private slots:
      void insertCompletion(const QString & completion);

   private:
      QString textUnderCursor() const;

   private:
      QCompleter * c = nullptr;
};
TextEdit::TextEdit(QWidget * parent): QTextEdit(parent) {
   setPlainText(tr("This TextEdit provides autocompletions for words that have more than"
         " 3 characters. You can trigger autocompletion using ") +
      QKeySequence("Ctrl+E").toString(QKeySequence::NativeText));
}
TextEdit::~TextEdit() {}
void TextEdit::setCompleter(QCompleter *completer)
{
    if (c)
        c->disconnect(this);

    c = completer;

    if (!c)
        return;

    c->setWidget(this);
    c->setCompletionMode(QCompleter::PopupCompletion);
    c->setCaseSensitivity(Qt::CaseInsensitive);
    QObject::connect(c, QOverload<const QString &>::of(&QCompleter::activated),
                     this, &TextEdit::insertCompletion);
}
QCompleter * TextEdit::completer() const {
   return c;
}