Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is my solution based on serge_gubenko's answer. This class uses QStringListModel, but it can be easily replaced with any other model.</p> <p><code>Completer_line_edit.h</code></p> <pre><code>#include &lt;QLineEdit&gt; #include &lt;QStringListModel&gt; #include &lt;QTimer&gt; /*! Line edit widget with auto completion based on QStringListModel. Modified behaviour: completion list will appear even when contents of line edit is empty. Full list of options will be showed when line edit has focus and is empty. */ class Completer_line_edit : public QLineEdit { Q_OBJECT public: explicit Completer_line_edit(QWidget *parent = 0); //! Set list of options used for copmletion. inline void set_list(QStringList list) { model.setStringList(list); } private: QStringListModel model; void focusInEvent(QFocusEvent *e); void customEvent(QEvent* e); QTimer timer; private slots: void slot_text_edited(); void slot_call_popup(); }; </code></pre> <p><code>Completer_line_edit.cpp</code></p> <pre><code>#include "Completer_line_edit.h" #include &lt;QCompleter&gt; #include &lt;QEvent&gt; #include &lt;QApplication&gt; Completer_line_edit::Completer_line_edit(QWidget *parent) : QLineEdit(parent) { setCompleter(new QCompleter()); completer()-&gt;setModel(&amp;model); completer()-&gt;setCompletionMode(QCompleter::PopupCompletion); completer()-&gt;setCaseSensitivity(Qt::CaseInsensitive); connect(this, SIGNAL(textEdited(QString)), this, SLOT(slot_text_edited())); } void Completer_line_edit::focusInEvent(QFocusEvent *e) { QLineEdit::focusInEvent(e); // force completion when line edit is focued in completer()-&gt;complete(); } void Completer_line_edit::slot_text_edited() { qDebug() &lt;&lt; "text edited"; // force to show all items when text is empty completer()-&gt;setCompletionMode(text().isEmpty()? QCompleter::UnfilteredPopupCompletion: QCompleter::PopupCompletion); if (text().isEmpty()) { // completion list will be hidden now; we will show it again after a delay timer.singleShot(100, this, SLOT(slot_call_popup())); } } void Completer_line_edit::slot_call_popup() { // apparently, complete() works only in event handler QApplication::postEvent(this, new QEvent(QEvent::User)); } void Completer_line_edit::customEvent(QEvent *e) { QLineEdit::customEvent(e); // force completion after text is deleted completer()-&gt;complete(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload