Created
April 18, 2018 23:54
-
-
Save CraftedCart/b790af76202514f44a1aa8fdfdb9b382 to your computer and use it in GitHub Desktop.
Vim Supertab like completion for a QCompleter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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