PDA

View Full Version : Invoke QCompletion on keyboard shortcut



aspidites
30th August 2009, 10:32
How do I invoke a QCompleter's completion list on a specific keyboard shortcut? Rather than have completions shown after each character is typed, I would like the completions to show only after the user has pressed Shift+Return.

I have gotten to the point where I can enable/disable autocompletion, but doing so doesn't invoke the completion popup until the next character is pressed.

Current Behavior:

User types the letter "c".
User presses the Shift+Return keyboard shortcut.
The use then types the letter "a".
A popup box with the words "cat", "cattle", and "cave" appears.


Desired Behavior:

User types the letter "c".
User presses the Shift+Return keyboard shortcut.
A popup box with the words "cat", "cattle", and "cave" appears.


In other words, the popup box shoudl appear immediately after the shortcut is pressed.

Here is the current (relevant) code:


...
class AutoCompleteFilter(QObject):
def __init__(self):
QObject.__init__(self)
self.isActive = False

def eventFilter(self, object, event):
if event.type() == QEvent.KeyPress:
if event.modifiers() and Qt.ControlModifier:
if event.key() == Qt.Key_Return:
if self.isActive:
self.isActive = False
self.emit(SIGNAL("autoCompleteToggled(bool)"), False)
else:
self.isActive = True
self.emit(SIGNAL("autoCompleteToggled(bool)"), True)

return True

return QObject.eventFilter(self, object, event)
...
class DeckEditorWindow(QMainWindow, Ui_DeckEditorWindow):

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

self.autoCompleteFilter = AutoCompleteFilter()

self.connect(self.autoCompleteFilter,
SIGNAL("autoCompleteToggled(bool)"),
self.autoComplete)

self.filtersLineEdit.installEventFilter(self.autoC ompleteFilter)
...
def autoComplete(self, isActive):
if isActive:
self.completer = QCompleter(self)
self.completer.setModel(self.resultsModel)
self.completer.setModelSorting(QCompleter.CaseInse nsitivelySortedModel)
if isActive:
self.completer = QCompleter(self)
self.completer.setModel(self.resultsModel) self.completer.setModelSorting(QCompleter.CaseInse nsitivelySortedModel)
self.completer.setCaseSensitivity(Qt.CaseInsensiti ve)
self.completer.setCompletionRole(Qt.DisplayRole)
self.filtersLineEdit.setCompleter(self.completer)
self.completer.complete()
else:
self.filtersLineEdit.setCompleter(None)
...



I've tried messing with the complete() and popup().show() methods with mixed results. Any help is greatly appreciated.