PDA

View Full Version : Combobox entries filter as I type



yazwas
8th August 2009, 12:19
Hello everyone

I have a large data-set that I populate to a combo box, what I would like to do, is to filter the results of that set as I type, so if i type something like Hello, all the entries that contains the word "hello", would only appear in the combo box

How to do this thing with combo-box and is using a combo box the best method to implement this? :confused: or if you think some other method is more appropriate, I would love to hear about it!

any suggestions are most welcomed!

thanks for your help!

Lykurg
8th August 2009, 13:09
You could use a normal QLineEdit with a QCompleter.

yazwas
8th August 2009, 13:55
Thanks Lykurg

but I also want the user to be able to see all the entries, is that possible with the QLineEdit?

thanks

numbat
8th August 2009, 15:04
You can use a QSortFilterProxyModel Something like:


QStringListModel * model = new QStringListModel;

for (int x = 0; x < 1000; x++)
{
model->insertRow(model->rowCount());
model->setData(model->index(model->rowCount() - 1, 0), QString("hello%1").arg(x), Qt::DisplayRole);
}

QComboBox * cb = new QComboBox;

QSortFilterProxyModel * proxy = new QSortFilterProxyModel;
proxy->setSourceModel(model);

cb->setModel(proxy);
cb->setEditable(true);
cb->setCompleter(0);

// When the edit text changes, use it to filter the proxy model.
connect(cb, SIGNAL(editTextChanged(QString)), proxy, SLOT(setFilterWildcard(QString)));