2009-12-18 127 views
7

我使用QCompleter和QLineEdit,並且我想動態更新QCompleter的模型。即模型的內容根據QLineEdit的文本更新。如何動態更新QCompleter的模型

1)mdict.h

#include <QtGui/QWidget> 

class QLineEdit; 
class QCompleter; 
class QModelIndex; 

class mdict : public QWidget 
{ 
    Q_OBJECT 

public: 
    mdict(QWidget *parent = 0); 
    ~mdict() {} 

private slots: 
    void on_textChanged(const QString &text); 

private: 
    QLineEdit *mLineEdit; 
    QCompleter *mCompleter; 
}; 

2)mdict.cpp

#include <cassert> 
#include <QtGui> 
#include "mdict.h" 

mdict::mdict(QWidget *parent) : QWidget(parent), mLineEdit(0), mCompleter(0) 
{ 
    mLineEdit = new QLineEdit(this); 
    QPushButton *button = new QPushButton(this); 
    button->setText("Lookup"); 

    QHBoxLayout *layout = new QHBoxLayout(this); 
    layout->addWidget(mLineEdit); 
    layout->addWidget(button); 
    setLayout(layout); 

    QStringList stringList; 
    stringList << "m0" << "m1" << "m2"; 
    QStringListModel *model = new QStringListModel(stringList); 
    mCompleter = new QCompleter(model, this); 
    mLineEdit->setCompleter(mCompleter); 

    mLineEdit->installEventFilter(this); 

    connect(mLineEdit, SIGNAL(textChanged(const QString&)), 
      this, SLOT(on_textChanged(const QString&))); 
} 

void mdict::on_textChanged(const QString &) 
{ 
    QStringListModel *model = (QStringListModel*)(mCompleter->model()); 
    QStringList stringList; 
    stringList << "h0" << "h1" << "h2"; 
    model->setStringList(stringList); 
} 

我希望當我輸入 「H」,它應該給我 「H0」 一個自動完成列表,「h1」和「h2」,我可以使用keyborad選擇項目。但它不像我預期的那樣。

看來應該在QLineEdit發出「textChanged」信號之前設置模型。一種方法是重新實現「keyPressEvent」,但它涉及許多條件來獲取QLineEdit的文本。

所以,我想知道是否有一種簡單的方法來動態更新QCompleter的模型?

+0

我已經試過事件,它的工作原理,但它是不容易的使用有這麼多類型的按鍵(例如Backspace鍵......)的。在qlinecontrol.cpp中,你可以看到更多細節。我只是想知道它能以簡單的方式完成嗎? – 2009-12-18 15:28:57

回答

4

哦,我已經找到了答案:

使用信號 「textEdited」 而不是 「框TextChanged」。

調試QT的源代碼告訴我答案。

+0

這對我來說也有些幫助,但是當我試圖在目錄中深入挖掘時仍然遇到分段錯誤,請查看[paste bin](http://pastebin.com/Jxziq8hW)上的代碼, .. – 2013-11-04 10:38:29

1

您可以使用這樣的事情:

Foo:Foo() 
{ 
    ... 
    QLineEdit* le_foodName = new QLineEdit(this); 
    QCompleter* foodNameAutoComplete = new QCompleter(this); 
    le_foodName->setCompleter(foodNameAutoComplete); 

    updateFoodNameAutoCompleteModel(); 
    ... 
} 

// We call this function everytime you need to update completer 
void Foo::updateFoodNameAutoCompleteModel() 
{ 
    QStringListModel *model; 
    model = (QStringListModel*)(foodNameAutoComplete->model()); 
    if(model==NULL) 
     model = new QStringListModel(); 

    // Get Latest Data for your list here 
    QStringList foodList = dataBaseManager->GetLatestFoodNameList() ; 

    model->setStringList(foodList); 
    foodNameAutoComplete->setModel(model); 
} 
0

使用filterMode : Qt::MatchFlags財產。該屬性保存如何執行過濾。如果filterMode設置爲Qt::MatchStartsWith,則僅顯示以輸入字符開頭的條目。 Qt::MatchContains將顯示包含輸入字符的條目,並以Qt::MatchEndsWith顯示以輸入字符結尾的條目。 目前,只有這三種模式實施。將filterMode設置爲任何其他Qt::MatchFlag將發出警告,並且不會執行任何操作。默認模式是Qt::MatchStartsWith

該屬性是在Qt 5.2中引入的。

訪問功能:

Qt::MatchFlags filterMode() const 
void setFilterMode(Qt::MatchFlags filterMode)