Skip to content

Instantly share code, notes, and snippets.

@CraftedCart
Created April 18, 2018 23:54
Show Gist options
  • Save CraftedCart/b790af76202514f44a1aa8fdfdb9b382 to your computer and use it in GitHub Desktop.
Save CraftedCart/b790af76202514f44a1aa8fdfdb9b382 to your computer and use it in GitHub Desktop.
Vim Supertab like completion for a QCompleter
//Somewhere when you create your completer
completer->popup()->installEventFilter(this);
bool CommandWidget::eventFilter(QObject *obj, QEvent *event) {
if (obj == completer->popup()) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Tab) {
//Select the next item in the completion list when tab is pressed - Supertab style
QModelIndex index = completer->popup()->currentIndex();
QModelIndex newIndex;
if (index.row() == -1) {
//Nothing's selected - select the first entry
newIndex = completer->popup()->model()->index(0, 0);
} else {
//Something's already selected - select the next one
//Going off the end of the list will automatically deselect everything
newIndex = completer->popup()->model()->index(index.row() + 1, index.column());
}
completer->popup()->setCurrentIndex(newIndex);
return true;
}
}
}
//Pass the event on to the parent class
return QWidget::eventFilter(obj, event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment