Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    3
    6286
    Loading More Posts
    • 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
      LaserGuided last edited by

      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 Reply Quote 0
      • G
        goetz last edited by

        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 Reply Quote 0
        • L
          LaserGuided last edited by

          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 Reply Quote 0
          • First post
            Last post