[SOLVED] Trying to use QCompleter in Inline mode within a custom QTextEdit

  • I'm writing an XML editor and would like to add auto completion for closing tags and schema suggestions for opening tags.

    The minimal sample works perfectly in QCompleter::PopupCompletion mode. When the mode is set to QCompleter::InlineCompletion, I cannot get my QTextEdit to display highlighted suggestions like the QLineEdit does. Does QTextEdit insert completions inline like QLineEdit does, or do I have to implement my own functions to do this?

    Header file:

    #include <QTextEdit>

    class QCompleter;

    class CustomTextEdit : public QTextEdit

    CustomTextEdit( QWidget* parent = 0 );

    private slots:
    void insertCompletion( const QString& completion );

    void keyPressEvent( QKeyEvent* event );
    QString textUnderCursor()const;

    QCompleter* m_completer;


    #endif // CUSTOMTEXTEDIT_H

    Implementation file:
    #include "customtextedit.h"

    #include <QtGui>

    CustomTextEdit::CustomTextEdit( QWidget* parent )
    : QTextEdit( parent )
    m_completer = new QCompleter( this );
    m_completer->setWidget( this );

    QStringList words;
    words << "working" << "properly";
    QStringListModel* model = new QStringListModel( words, m_completer );
    m_completer->setModel( model );
    m_completer->setCompletionMode( QCompleter::PopupCompletion );
    connect( m_completer, SIGNAL( activated( QString ) ),
             this, SLOT( insertCompletion( QString ) ) );


    void CustomTextEdit::insertCompletion( const QString& completion )
    QTextCursor cursor = textCursor();
    int extra = completion.length() - m_completer->completionPrefix().length();
    cursor.movePosition( QTextCursor::Left );
    cursor.movePosition( QTextCursor::EndOfWord );
    cursor.insertText( completion.right( extra ) );
    setTextCursor( cursor );

    QString CustomTextEdit::textUnderCursor() const
    QTextCursor cursor = textCursor();
    cursor.select( QTextCursor::WordUnderCursor );
    return cursor.selectedText();

    void CustomTextEdit::keyPressEvent( QKeyEvent* event )
    if ( m_completer->popup()->isVisible() )
    switch ( event->key() )
    case Qt::Key_Enter:
    case Qt::Key_Return:
    case Qt::Key_Escape:
    case Qt::Key_Tab:

    QTextEdit::keyPressEvent( event );
    const QString completionPrefix = textUnderCursor();
    if ( completionPrefix != m_completer->completionPrefix() )
        m_completer->setCompletionPrefix( completionPrefix );
        m_completer->popup()->setCurrentIndex( m_completer->completionModel()->index( 0, 0 ) );
    if ( !event->text().isEmpty() && completionPrefix.length() > 2 )



  • From a look at the sources, it seems, that inline completion is not supported in QTextEdit out of the box. You will have to add the necessary functionality yourself using QCompleter's signals highlighted and activated (have a look at QLineEdits source code for some hints).

  • Thanks Volker! It's good to know I didn't overlook something.
    I'll use the popup setting until I feel the need to get fancy with an inline implementation.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.