2

I'm trying to create a subclass for QCombobox and QCompleter to achieve the following:

I want to make editable combobox which pops-up completion list when user starts typing.

Ex: Completion List - QStringList<<"manual"<<"anual" When I type 'anu', I want both "manual" and "anual" to appear in the completion drop-down list, because they both contain "anu". But instead I'm getting only **anu**al with al as inline completion so in the completion list I got only "anual". When I remove the inline suggestion with backspace then I'm getting full list as expected.

Why is the default inline completion appearing?

linecompleter.h

#ifndef LINECOMPLETER_H
#define LINECOMPLETER_H

#include <QtGui>
#include <QtWidgets>

class QKeyEvent;
class LineCompleter : public QCompleter
{
    Q_OBJECT

public:
    inline LineCompleter(const QStringList& words, QObject * parent) :
            QCompleter(parent), m_list(words), m_model()
    {
        setModel(&m_model);
    }

    inline void update(QString word)
    {
        // Do any filtering you like.
        // Here we just include all items that contain word.
        QStringList filtered = m_list.filter(word, caseSensitivity());
        m_model.setStringList(filtered);
        m_word = word;
        complete();
    }

    inline QString word()
    {
        return m_word;
    }

private:
    QStringList m_list;
    QStringListModel m_model;
    QString m_word;
};

class PageLineEdit : public QComboBox
{
    Q_OBJECT

public:
    PageLineEdit(QWidget *parent = 0);
    ~PageLineEdit();

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

protected:
    void keyPressEvent(QKeyEvent *e);

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

private:
    LineCompleter *c;
};

#endif // LINECOMPLETER_H

linecompleter.cpp

#include "linecompleter.h"

PageLineEdit::PageLineEdit(QWidget *parent)
    : QComboBox(parent), c(0)
{
}

PageLineEdit::~PageLineEdit()
{
}

void PageLineEdit::setCompleter(LineCompleter *completer)
{
    if (c)
            QObject::disconnect(c, 0, this, 0);

        c = completer;

        if (!c)
            return;

        c->setWidget(this);
        c->setCompletionMode(QCompleter::PopupCompletion);
        c->setCaseSensitivity(Qt::CaseInsensitive);
        QObject::connect(c, SIGNAL(activated(QString)),
                         this, SLOT(insertCompletion(QString)));
}

LineCompleter *PageLineEdit::completer() const
{
    return c;
}

void PageLineEdit::insertCompletion(const QString& completion)
{
    lineEdit()->setText(completion);
    lineEdit()->selectAll();
}


void PageLineEdit::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:
            e->ignore();
            return; // Let the completer do default behavior
        default:
                   break;
        }
    }

    bool isShortcut = (e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E;
    if (!isShortcut)
        QComboBox::keyPressEvent(e); // Don't send the shortcut (CTRL-E) to the text edit.

    if (!c)
        return;

    bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!isShortcut && !ctrlOrShift && e->modifiers() != Qt::NoModifier)
    {
        c->popup()->hide();
        return;
    }

    c->update(lineEdit()->text());
    c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));

}

I implemented this using this Qt centre thread

I'm using Qt 4.8 which doesn't have setFilterMode() for QCompleter.

How can I subclass the QCombobox properly to achieve this required feature?

Ghost
  • 249
  • 1
  • 14
Pramod
  • 121
  • 2
  • 16

0 Answers0