gpt4 book ai didi

c++ - 使用 QComboBox C++ 进行过滤

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:16:32 24 4
gpt4 key购买 nike

我想创建一个可编辑的 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) 插槽)来解决。如果只有一项(不确定是否可以),则完成者完成输入。有时复选框会使所有项目加倍(不知道为什么)。如果这个问题很严重,我认为您需要从头开始编写自定义 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com