我想创建一个可编辑的QComboBox,它根据搜索查询过滤结果并相应地更新下拉列表。

阅读How do I Filter the PyQt QCombobox Items based on the text input?之后,我尝试在C++中实现类似的功能。

但是我现在无法在QComboBox中存储任何内容。即使在通过addItem()添加新条目之后,总数仍为0。

这是什么原因,以及如何使用QSortFilterProxyModel在QComboBox中插入条目?

以下是相关代码段:

SearchBox = new QComboBox(this);
SearchBox->setEditable(true);

// Try adding a few entries and check if they persist after changing the model
SearchBox->addItem(QString("hi"));
SearchBox->addItem(QString("bye"));

int count = SearchBox->count();    // count = 2

ProxyModel = new QSortFilterProxyModel;
ProxyModel->setSourceModel(SearchBox->model());
ProxyModel->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
SearchBox->setModel(ProxyModel);

// Check count again
count = SearchBox->count();    // count = 0     <- Why?

// Try adding new entries
SearchBox->addItem(QString("Hi again"));

count = SearchBox->count();    // count = 0  .. So new entries don't get stored


Completer = new QCompleter(ProxyModel,this);
Completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
SearchBox->setCompleter(Completer);


QObject::connect(SearchBox->lineEdit(), SIGNAL(textChanged(const QString)), ProxyModel, SLOT(setFilterFixedString(const QString)));
QObject::connect(Completer, SIGNAL(activated(const QString &)), this, SLOT(onCompleterActivated(const QString &)));

最佳答案

使用QStringListModel存储项目。如果代理模型中没有项目(如果过滤器字符串过滤出所有项目),则应用程序崩溃(这需要进一步调查-这是更完整的问题还是组合框)。可以通过不应用此类过滤器(onTextChanged(QString text)插槽)来解决此问题。如果只有一项(不是确定的话),则Completer完成输入。有时,复选框会将所有项目加倍(不知道为什么)。如果这个问题很关键,我认为您需要从头开始编写自定义ComboBox,这是一项严肃的工作。

{
    SearchBox = new QComboBox(this);
    SearchBox->setEditable(true);

    QStringList Items;
    Items << "hi" << "bye";
    StringListModel = new QStringListModel();
    StringListModel->setStringList(Items);

    ProxyModel = new QSortFilterProxyModel;
    ProxyModel->setSourceModel(StringListModel);
    ProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    SearchBox->setModel(ProxyModel);

    // Check count again
    int count = SearchBox->count();    // count = 2

    // Try adding new entries
    QStringList Items_ = StringListModel->stringList();
    Items_ << "hi again";
    StringListModel->setStringList(Items_);

    count = SearchBox->count();    // count = 3

    Completer = new QCompleter(ProxyModel,this);
    Completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
    SearchBox->setCompleter(Completer);

    QObject::connect(SearchBox->lineEdit(), SIGNAL(textChanged(const QString)), this, SLOT(onTextChanged(QString)));
    QObject::connect(Completer, SIGNAL(activated(const QString &)), this, SLOT(onCompleterActivated(const QString &)));
}

void MainWindow::onTextChanged(QString Text) {
    QStringList Items = StringListModel->stringList();
    QString Item;
    foreach(Item,Items) {
        if (Item.indexOf(Text) > -1) {
            ProxyModel->setFilterFixedString(Text);
            return;
        }
    }
}

关于c++ - 用QComboBox C++过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32142411/

10-16 08:16