我对QCompleter进行了子类化,以提供一些特殊功能。我希望在模型中只有一个具有给定前缀的补全时被触发,但那并不是我遇到的问题。

我在子类中创建了一个虚拟的setCompleterPrefix(),但是编译器似乎没有注意到它。当用户输入前缀时,将调用基本QCompleter::setCompletionPrefix()。难道我做错了什么?

这是我的课:

#ifndef INSTANTCOMPLETER_H
#define INSTANTCOMPLETER_H

#include <QCompleter>

namespace Reliant
{
class InstantCompleter : public QCompleter
{
    Q_OBJECT
public:
    explicit InstantCompleter(QObject* parent = 0);

private:

signals:

public slots:
    virtual void setCompletionPrefix(const QString &prefix);

};
}

#endif // INSTANTCOMPLETER_H

定义:
#include "instantcompleter.h"
using Reliant::InstantCompleter;

InstantCompleter::InstantCompleter(QObject* parent) :
    QCompleter(parent)
{
}

void InstantCompleter::setCompletionPrefix(const QString &prefix)
{
    int completionCount = this->completionCount();
    if(completionCount == 1 && setCurrentRow(0))
        emit activated(currentCompletion());
    else
        QCompleter::setCompletionPrefix(prefix);
}

最佳答案

您可以使用QCompleter::completionModel()返回的模型及其信号来跟踪完成计数:

InstantCompleter::InstantCompleter(QObject* parent) :
    QCompleter(parent)
{
    connect(completionModel(), SIGNAL(layoutChanged()), SLOT(completionModelChanged()));
}

// declared in the "private slots:" section
void InstantCompleter::completionModelChanged()
{
    if (completionCount() == 1 && setCurrentRow(0))
        emit activated(currentCompletion());
}

09-07 06:54