I am developing a simple code editor for a language that mainly constists of assignments in the form:
key=value

I already archived syntax highlighting using a custom QSyntaxHighlighter. Now I want to add simple auto completion using QCompleter as shown in the Qt example "Custom Completer Example" that comes with Qt 4.2.

I got this to work but I still have three questions:

1. Could somebody please explain me the following marked lines of code from the textedit.cpp example file?

Qt Code:
  1. void TextEdit::keyPressEvent(QKeyEvent *e)
  2. {
  3. if (c && c->popup()->isVisible()) {
  4. // The following keys are forwarded by the completer to the widget
  5. switch (e->key()) {
  6. case Qt::Key_Enter:
  7. case Qt::Key_Return:
  8. case Qt::Key_Escape:
  9. case Qt::Key_Tab:
  10. case Qt::Key_Backtab:
  11. e->ignore();
  12. return; // let the completer do default behavior
  13. default:
  14. break;
  15. }
  16. }
To copy to clipboard, switch view to plain text mode 

When the completer popup is visible the above keys are forwarded to the completer (by ignoring them in the text edit)?

Qt Code:
  1. bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
  2. if (!c || !isShortcut) // dont process the shortcut when we have a completer
  3. QTextEdit::keyPressEvent(e);
To copy to clipboard, switch view to plain text mode 

The comment is somewhat misleading!? If there is no completer installed or the shortcut was not triggered by the key event then they let the original QTextEdit event handlder handle the event?

Qt Code:
  1. const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
  2. if (!c || (ctrlOrShift && e->text().isEmpty()))
  3. return;
To copy to clipboard, switch view to plain text mode 

Hmm? What is ctrlOrShift needed for? What do they do here?

Qt Code:
  1. static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
  2. bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
  3. QString completionPrefix = textUnderCursor();
To copy to clipboard, switch view to plain text mode 

What is hasModifier needed for?

Qt Code:
  1. if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
  2. || eow.contains(e->text().right(1)))) {
  3. c->popup()->hide();
  4. return;
  5. }
To copy to clipboard, switch view to plain text mode 

Hide the popup is the completion length is fallen below or the key is contained inside the end-of-word string (for my problem this would only contain "="?). Why are they looking at hasModifier and e->text().isEmpty()?

Qt Code:
  1. if (completionPrefix != c->completionPrefix()) {
  2. c->setCompletionPrefix(completionPrefix);
  3. c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
  4. }
  5. QRect cr = cursorRect();
  6. cr.setWidth(c->popup()->sizeHintForColumn(0)
  7. + c->popup()->verticalScrollBar()->sizeHint().width());
  8. c->complete(cr); // popup it up!
  9. }
To copy to clipboard, switch view to plain text mode 

Initialize the completer with the current completionPrefix and show it at the position cr (the cursor position?).

2. The key sequence to pop up the completer is hardcoded in the example. How would I implement a configurable key sequence? I found QKeySequence, but no way to match it in the keyEvent. The QKeyEvent itself can only match against standard sequences defined by Qt.

3. I would like to add completion based on the key string (context). How would I implement this? The Trolls mention QSyntaxHighlighter::setCurrentBlockUserData as a way to do parenthesis matching and highlighting without giving a full example. I think I could use this technique to store the key/context while highlighting and use this information when showing the completer? How would I do this? Are there examples on how to do this (must not be completion, maybe parenthesis matching/highlighting)?

Thanks in advance,
-Jens