Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Trying to use QCompleter in Inline mode within a custom QTextEdit

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 6.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LaserGuided
    wrote on 21 Apr 2011, 18:16 last edited by
    #1

    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:
    @
    #ifndef CUSTOMTEXTEDIT_H
    #define CUSTOMTEXTEDIT_H

    #include <QTextEdit>

    class QCompleter;

    class CustomTextEdit : public QTextEdit
    {
    Q_OBJECT

    public:
    CustomTextEdit( QWidget* parent = 0 );

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

    private:
    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:
    event->ignore();
    return;
    }
    }

    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 )
        m_completer->complete();
    

    }

    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 26 Apr 2011, 10:05 last edited by
      #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).

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LaserGuided
        wrote on 28 Apr 2011, 21:15 last edited by
        #3

        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.

        1 Reply Last reply
        0

        2/3

        26 Apr 2011, 10:05

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved