I made a QLineEdit
for reading an infix maths expression. Operators are limited to the +-*/
and brackets. Values can be numeric or a variable name representing a numeric value. I want to autocomplete for variable names.
The problem is that apparently simple QComplete
only works for single pre-defined words/phrases. They don't work in between other words (As you might expect to do when modifying an expression).
I tried reading the Tree Model Completer, but since I'm programming in Python that wasn't too helpful to me. Does anyone know of a simple Tree Model Completer example coded in python?
After reading ekhumoros comment I decided to make a short example for a custom Completer.
Here is the example:
from PySide import QtGui
class CustomCompleter(QtGui.QCompleter):
def __init__(self):
super().__init__()
def splitPath(self, path):
if path.endswith('ha'):
self.setModel(QtGui.QStringListModel([path + 'llo']))
return [path]
app = QtGui.QApplication([])
e = QtGui.QLineEdit()
c = CustomCompleter()
e.setCompleter(c)
e.show()
app.exec_()
Everytime the text ends with 'ha' it proposes to continue it with 'llo'. It looks for example like:
All of the work is done in splitPath(path)
of QCompleter
which is called twice(?) everytime I change the text of my edit field. After some processing of the text one should set the model new with a simple string list containing one or more proposals. It seems the model has to be set again everytime. See also QCompleter Custom Completion Rules.
This is not yet the full formula parsing and variable names completion, but a reasonable step towards this. It just explains how QCompleter
can be used for that goal. To summarize: Subclass QCompleter
and put all your custom logic into splitpath()
.