PDA

View Full Version : QComboBox autocomplete



aekilic
13th April 2009, 09:01
Dear All

We have problem for the combo box

We loead the combo like



QComboBox *cmbBox = Concept::ui.cmbItems;
QSqlQueryModel *_model = new QSqlQueryModel;
_model->setQuery("select countryname, id from country");
cmbBox->clear();
cmbBox->setModel(_model);


The combo is not editable.

The problem is when the users is writing on the combo, if he is not fast enough the word order change

like

e 1 sec n 1 sec g - it will go to words eng....

but if you print like

e 1 sec n 4 sec g - it will go to words g........

And the combo cant be editable?
Can any body help?

lyuts
13th April 2009, 09:21
Try to create your own completer by sublclassing QCompleter and setting it for your Combobox.

spirit
13th April 2009, 09:26
QSqlQueryModel can not be editable
from docs


The QSqlQueryModel class provides a read-only data model for SQL result sets.

aekilic
13th April 2009, 09:53
Dear Sprit

Problem is, I dont want that to be editable. And yes you are correct it is not editable.

But if I change the combo editable, if user change the curser with tab, it doesnt get the id for the country

Lykurg
13th April 2009, 10:04
Subclass QComboBox and filter key press event. Save the current "search string" in a member variable and perform the search in your QSqlQueryModel or jump to the matching item. Use QTimer to terminate the elapsed time while last key press and clear your internal string if necessary.

aekilic
13th April 2009, 10:13
was little bit complex?

Lykurg
13th April 2009, 10:25
Something like that:

// QTime m_time;
// QString m_search;
void MyComboBox::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down /* || ... */)
QComboBox::keyPressEvent(event);
else
{
if (m_time.elapsed() > 10)
m_search.clear();
m_search.append(event->text());
m_time.restart();
performSearch(); // handles the selection chance of the box according to m_search via setting the querymodel or use QComboBox::findText
}
}

spirit
13th April 2009, 10:31
Something like that:

I would made a little bit simpler


void MyComboBox::keyPressEvent(QKeyEvent *event)
{
const QString text = event->text();
if (text.isEmpty())
QComboBox::keyPressEvent(event);
else
{
if (m_time.elapsed() > 10)
m_search.clear();
m_search.append(text);
m_time.restart();
performSearch(); // handles the selection chance of the box according to m_search via setting the querymodel or use QComboBox::findText
}

:)

aekilic
15th April 2009, 07:07
is this possible if I promote this from designer?

Lykurg
15th April 2009, 08:23
Of course. Just use a normal QComboBox and promote it to MyComboBox. If you want a working combo box in your designer preview you have to write a designer plugin with MyComboBox.

aekilic
15th April 2009, 09:57
Dear all

I think this will not solve my problem. Because,

My combo cant be editable.

My main problem is, I got combo box in a widget or in a delegate, and in the combo boxes I have lots of rows. And every row get an ID from sql when I load the combo.

Like 'country name', and ID, I save these ID to the database. Problem is If I leave the combo not editable, it take to much time to find it in the combo, because there is no write delay, I mean you have to write really fast to do that.

If I make it editable, if users passes the combo without pressing return, the ID doesnt change but the text seems to be changed, so the users cant write it on the database

This problem is my main problem in this project!

Lykurg
15th April 2009, 10:08
Is setEditable(false) blocking the keyPressEvent??? Don't get your problem. If you think finding a item in the box takes to long time (blocks your GUI) use a thread for it. But really a combo box can't be too huge.

aekilic
15th April 2009, 10:29
let me tell you

like in combo I have 2000 items

let me tell you country name and ids,

I make the combo editable, in that case I could write and find in combo, but in that case if the users passes the combo without pressing return key, the id doesnt change, it stays the old id

Lykurg
15th April 2009, 10:58
let me tell you

like in combo I have 2000 items A normal CPU says: haha, that's no size for me...:p So finding items won't take long l'time


I make the combo editable, in that case I could write and find in combo,
You don't need to set the box ediatable. Then user can navigate to the item also by typing. To avoid too short "input cleaning time" use the solution mentioned above.

but in that case if the users passes the combo without pressing return key, the id doesnt change, it stays the old id
An editable combo box doesn't intend to provide a solution to your problem. Your need can better be fitted by an QLineEdit with an installed QCompleter holding the countries. Or a QlineEdit as search fiel over an QListView etc...



but in that case if the users passes the combo without pressing return key, the id doesnt change, it stays the old id

Then check if the user has edited the values and call press yourself before using the value.

despeboy
13th May 2009, 15:23
You might also want to have a look at:
http://doc.trolltech.com/4.5/qapplication.html#keyboardInputInterval-prop