QTextEdit autocompletion

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()

Why not just use a combobox and set the model to a QStringListModel ? It has all of this functionality already.

Doesnt a combobox only autofill for one word. What if you have multiple strings and need autocomplete?

like substring matches?

completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)

Will work no?

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 see, i played around with my suggesting and no dice. It’s an interesting idea, share the tool when you’re done if you can :slight_smile:

You can download a first version of the script here http://josbalcaen.com/scripts/maya/gotyping/