Results 1 to 2 of 2

Thread: QTextEdit with QCompleter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2006
    Qt products
    Unix/X11 Windows

    Default QTextEdit with QCompleter

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

    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,

  2. #2
    Join Date
    Nov 2006
    Qt products
    Unix/X11 Windows

    Default Re: QTextEdit with QCompleter

    And another question reguarding the Custom Completer Example:

    I type at least three letters to have the completer pop up (e.g. add) then I enter a point that will hide the completer as wished. I enter some charater and delete it until the point. The completer will now pop up again giving me hints that do not make sense as I already typed the point (end of word char). If I select one of the hints that is longer than "add." it will complete "".

    I assume I would have to do some changes to QString TextEdit::textUnderCursor() const to return only the chars after an end of word char?
    Or can I set the completer to be strict and not to show hints when the completionPrefix does not match any string? (This happens also with other chatrs like =, ?, ...)


Similar Threads

  1. Re-implement mouse events of QTextEdit
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2006, 16:55
  2. QCompleter with QTextEdit
    By ePharaoh in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2006, 13:03
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  4. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40
  5. Obtaining clean (x)html from QTextEdit
    By ccf_h in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2006, 14:47


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.