Viadeo Twitter Google Bookmarks ! Facebook Digg MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Custom Completer Example


The Custom Completer example shows how to provide string-completion facilities for an input widget based on data provided by a model. The completer pops up suggestions for possible words based on the first three characters input by the user and the user's choice of word is inserted into the TextEdit using QTextCursor.

Setting Up The Resource File

The Custom Completer example requires a resource file, wordlist.txt, that has a list of words to help QCompleter complete words. This file contains the following:

 <!DOCTYPE RCC><RCC version="1.0">
 <qresource prefix="/">

TextEdit Class Definition

The TextEdit class is a subclass of QTextEdit with a custom insertCompletion() slot and it reimplements the keyPressEvent() and the focusInEvent() functions. TextEdit also contains a private function textUnderCursor() and a private instance of QCompleter, c.

 class TextEdit : public QTextEdit

     TextEdit(QWidget *parent = 0);

     void setCompleter(QCompleter *c);
     QCompleter *completer() const;

     void keyPressEvent(QKeyEvent *e);
     void focusInEvent(QFocusEvent *e);

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

     QString textUnderCursor() const;

     QCompleter *c;

TextEdit Class Implementation

The constructor for TextEdit constructs a TextEdit with a parent and initializes c. The instructions to use the completer is displayed on the TextEdit object, using the setPlainText() function.

 TextEdit::TextEdit(QWidget *parent)
 : QTextEdit(parent), c(0)
     setPlainText(tr("This TextEdit provides autocompletions for words that have more than"
                     " 3 characters. You can trigger autocompletion using ") +

In addition, TextEdit also includes a default destructor:


The setCompleter() function accepts a completer and sets it up. We use if (c) to check if c has been initialized. If it has been initialized, the QObject::disconnect() function is invoked to disconnect the signal from the slot. This is to ensure that no previous completer object is still connected to the slot.

 void TextEdit::setCompleter(QCompleter *completer)
     if (c)
         QObject::disconnect(c, 0, this, 0);

     c = completer;

     if (!c)

     QObject::connect(c, SIGNAL(activated(QString)),
                      this, SLOT(insertCompletion(QString)));

We then instantiate c with completer and set it as TextEdit's widget. The completion mode and case sensitivity are also set and then we connect the activated() signal to the insertCompletion() slot.

The completer() function is a getter function that returns c.

 QCompleter *TextEdit::completer() const
     return c;

The completer pops up the options available, based on the contents of wordlist.txt, but the text cursor is responsible for filling in the missing characters, according to the user's choice of word.

Suppose the user inputs "ACT" and accepts the completer's suggestion of "ACTUAL". The completion string is then sent to insertCompletion() by the completer's activated() signal.

The insertCompletion() function is responsible for completing the word using a QTextCursor object, tc. It validates to ensure that the completer's widget is TextEdit before using tc to insert the extra characters to complete the word.

 void TextEdit::insertCompletion(const QString& completion)
     if (c->widget() != this)
     QTextCursor tc = textCursor();
     int extra = completion.length() - c->completionPrefix().length();

The figure below illustrates this process:

completion.length() = 6


The difference between these two values is extra, which is 3. This means that the last three characters from the right, "U", "A", and "L", will be inserted by tc.

The textUnderCursor() function uses a QTextCursor, tc, to select a word under the cursor and return it.

 QString TextEdit::textUnderCursor() const
     QTextCursor tc = textCursor();;
     return tc.selectedText();

The TextEdit class reimplements focusInEvent() function, which is an event handler used to receive keyboard focus events for the widget.

 void TextEdit::focusInEvent(QFocusEvent *e)
     if (c)

The keyPressEvent() is reimplemented to ignore key events like Qt::Key_Enter, Qt::Key_Return, Qt::Key_Escape, Qt::Key_Tab, and Qt::Key_Backtab so the completer can handle them.

If there is an active completer, we cannot process the shortcut, Ctrl+E.

 void TextEdit::keyPressEvent(QKeyEvent *e)
     if (c && c->popup()->isVisible()) {
         // The following keys are forwarded by the completer to the widget
        switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:
             return; // let the completer do default behavior

     bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
     if (!c || !isShortcut) // dont process the shortcut when we have a completer

We also handle other modifiers and shortcuts for which we do not want the completer to respond to.

     const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
     if (!c || (ctrlOrShift && e->text().isEmpty()))

     static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
     bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
     QString completionPrefix = textUnderCursor();

     if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
                       || eow.contains(e->text().right(1)))) {

     if (completionPrefix != c->completionPrefix()) {
         c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
     QRect cr = cursorRect();
                 + c->popup()->verticalScrollBar()->sizeHint().width());
     c->complete(cr); // popup it up!

Finally, we pop up the completer.

MainWindow Class Definition

The MainWindow class is a subclass of QMainWindow and implements a private slot, about(). This class also has two private functions, createMenu() and modelFromFile() as well as private instances of QCompleter and TextEdit.

 class MainWindow : public QMainWindow

     MainWindow(QWidget *parent = 0);

 private slots:
     void about();

     void createMenu();
     QAbstractItemModel *modelFromFile(const QString& fileName);

     QCompleter *completer;
     TextEdit *completingTextEdit;

MainWindow Class Implementation

The constructor constructs a MainWindow with a parent and initializes the completer. It also instantiates a TextEdit and sets its completer. A QStringListModel, obtained from modelFromFile(), is used to populate the completer. The MainWindow's central widget is set to TextEdit and its size is set to 500 x 300.

 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent), completer(0)

     completingTextEdit = new TextEdit;
     completer = new QCompleter(this);

     resize(500, 300);

The createMenu() function creates the necessary QAction objects needed for the "File" and "Help" menu and their triggered() signals are connected to the quit(), about(), and aboutQt() slots respectively.

 void MainWindow::createMenu()
     QAction *exitAction = new QAction(tr("Exit"), this);
     QAction *aboutAct = new QAction(tr("About"), this);
     QAction *aboutQtAct = new QAction(tr("About Qt"), this);

     connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

     QMenu* fileMenu = menuBar()->addMenu(tr("File"));

     QMenu* helpMenu = menuBar()->addMenu(tr("About"));

The modelFromFile() function accepts a fileName and attempts to extract the contents of this file into a QStringListModel. We display the Qt::WaitCursor when we are populating the QStringList, words, and restore the mouse cursor when we are done.

 QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
     QFile file(fileName);
     if (!
         return new QStringListModel(completer);

 #ifndef QT_NO_CURSOR
     QStringList words;

     while (!file.atEnd()) {
         QByteArray line = file.readLine();
         if (!line.isEmpty())
             words << line.trimmed();

 #ifndef QT_NO_CURSOR
     return new QStringListModel(words, completer);

The about() function provides a brief description about the Custom Completer example.

 void MainWindow::about()
     QMessageBox::about(this, tr("About"), tr("This example demonstrates the "
         "different features of the QCompleter class."));

main() Function

The main() function instantiates MainWindow and invokes the show() function.

 int main(int argc, char *argv[])

     QApplication app(argc, argv);
     MainWindow window;;
     return app.exec();


Best Of

Actualités les plus lues

  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 38
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  5. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
Page suivante

Le blog Digia au hasard


Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.



Liens utiles


  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !

Hébergement Web