4

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?

AAEM
  • 1,837
  • 2
  • 18
  • 26
Mark Ang
  • 149
  • 1
  • 11
  • So you want to auto-complete on variable names that have been previously entered in the expression, but are otherwise not pre-defined? If so, then just extract the relevant variable names on the fly, and then populate the completer from that. Most advanced text-editors have this feature: it's usually called "auto-complete from document" or suchlike. – ekhumoro Jan 21 '15 at 21:20
  • The variable names are already defined. I'm just looking for example code of a Tree Model Completer in PyQt. – Mark Ang Jan 21 '15 at 23:38
  • @ekhumoro That would not work because the completer checks the whole text already entered, it has no idea what is a variable name and what is the rest of the formula. – NoDataDumpNoContribution Feb 02 '15 at 11:59
  • Using a Tree Model Completer will not help you much here. What you need is to parse the formula at any given point to find out what is the current variable name stub and then invoke some sort of Completer on only that part and complete it. – NoDataDumpNoContribution Feb 02 '15 at 12:03
  • @Trilarion. I think I see what the confusion is now. When the OP says `QCompleter`, they actually mean the standard completion mechanism used by `QLineEdit`. But the `QCompleter` class itself is far more flexible than that. For a custom auto-completer, it will be necessary to use `QCompleter.setWidget()` and implement your own completion mechanism. But there's no need for a Tree Model, as you say - just simple "auto-complete from document" will do. – ekhumoro Feb 02 '15 at 19:06
  • @ekhumoro Thanks for the explanations. It put me on the right track to compile a small example as an answer. – NoDataDumpNoContribution Feb 02 '15 at 20:40

1 Answers1

3

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:

enter image description here

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

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104