Results 1 to 9 of 9

Thread: How to change completion rule of QCompleter

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to change completion rule of QCompleter

    Using this QCompleter example and this custom QCompleter example, I implemented what you want.

    Header:
    Qt Code:
    1. #ifndef LINEEDIT_H
    2. #define LINEEDIT_H
    3.  
    4. #include <QLineEdit>
    5. #include <QStringList>
    6. #include <QStringListModel>
    7. #include <QString>
    8. #include <QCompleter>
    9.  
    10. class MyCompleter : public QCompleter
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. inline MyCompleter(const QStringList& words, QObject * parent) :
    16. QCompleter(parent), m_list(words), m_model()
    17. {
    18. setModel(&m_model);
    19. }
    20.  
    21. inline void update(QString word)
    22. {
    23. // Do any filtering you like.
    24. // Here we just include all items that contain word.
    25. QStringList filtered = m_list.filter(word, caseSensitivity());
    26. m_model.setStringList(filtered);
    27. m_word = word;
    28. complete();
    29. }
    30.  
    31. inline QString word()
    32. {
    33. return m_word;
    34. }
    35.  
    36. private:
    37. QStringList m_list;
    38. QString m_word;
    39. };
    40.  
    41. class MyLineEdit : public QLineEdit
    42. {
    43. Q_OBJECT
    44.  
    45. public:
    46. MyLineEdit(QWidget *parent = 0);
    47. ~MyLineEdit();
    48.  
    49. void setCompleter(MyCompleter *c);
    50. MyCompleter *completer() const;
    51.  
    52. protected:
    53. void keyPressEvent(QKeyEvent *e);
    54.  
    55. private slots:
    56. void insertCompletion(const QString &completion);
    57.  
    58. private:
    59. MyCompleter *c;
    60. };
    61.  
    62. #endif // LINEEDIT_H
    To copy to clipboard, switch view to plain text mode 

    CPP:
    Qt Code:
    1. MyLineEdit::MyLineEdit(QWidget *parent)
    2. : QLineEdit(parent), c(0)
    3. {
    4. }
    5.  
    6. MyLineEdit::~MyLineEdit()
    7. {
    8. }
    9.  
    10. void MyLineEdit::setCompleter(MyCompleter *completer)
    11. {
    12. if (c)
    13. QObject::disconnect(c, 0, this, 0);
    14.  
    15. c = completer;
    16.  
    17. if (!c)
    18. return;
    19.  
    20. c->setWidget(this);
    21. connect(completer, SIGNAL(activated(const QString&)), this, SLOT(insertCompletion(const QString&)));
    22. }
    23.  
    24. MyCompleter *MyLineEdit::completer() const
    25. {
    26. return c;
    27. }
    28.  
    29. void MyLineEdit::insertCompletion(const QString& completion)
    30. {
    31. setText(completion);
    32. selectAll();
    33. }
    34.  
    35.  
    36. void MyLineEdit::keyPressEvent(QKeyEvent *e)
    37. {
    38. if (c && c->popup()->isVisible())
    39. {
    40. // The following keys are forwarded by the completer to the widget
    41. switch (e->key())
    42. {
    43. case Qt::Key_Enter:
    44. case Qt::Key_Return:
    45. case Qt::Key_Escape:
    46. case Qt::Key_Tab:
    47. case Qt::Key_Backtab:
    48. e->ignore();
    49. return; // Let the completer do default behavior
    50. }
    51. }
    52.  
    53. bool isShortcut = (e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E;
    54. if (!isShortcut)
    55. QLineEdit::keyPressEvent(e); // Don't send the shortcut (CTRL-E) to the text edit.
    56.  
    57. if (!c)
    58. return;
    59.  
    60. bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    61. if (!isShortcut && !ctrlOrShift && e->modifiers() != Qt::NoModifier)
    62. {
    63. c->popup()->hide();
    64. return;
    65. }
    66.  
    67. c->update(text());
    68. c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
    69. }
    To copy to clipboard, switch view to plain text mode 

    Usage:
    Qt Code:
    1. MyLineEdit * te = new MyLineEdit;
    2. MyCompleter * completer = new MyCompleter(QStringList() << "oneone" << "Twotwo", this);
    3. completer->setCaseSensitivity(Qt::CaseInsensitive);
    4. te->setCompleter(completer);
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to numbat for this useful post:

    akiross (17th April 2011), viet.nguyentien (13th September 2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.