PDA

View Full Version : QComboBox



coderbob
11th December 2007, 02:45
Can I have a QComboBox with a validator with invalid input and still have it editable?

To answer my own question; No.

So my question is there an easy workaround for this before I dig deeper into a solution.

I need to have a editable QComboBox with numeric input entries with attached explanation test.



QComboBox *cBox = new QComboBox(this);

cBox->setEditable(true);
QRegExp ireg("([0-9]{10})");
QRegExpValidator *iValidator = new QRegExpValidator(ireg,this);
cBoxt->setValidator(iValidator);

cBox->addItem(tr("1 - this an explanation"));
cBox->addItem(tr("2 - this also an explanation"));



The above will not function correctly due to the validator but I must maintain the ability to let the user enter a number that might not be present in the combobox.

Any suggestions?

Bob

mchara
11th December 2007, 06:35
You may write custom validator, note that validators have 3 states - invalid, INTERMEDIATE and acceptable.
You can also think about completer, it can be usefull too.

As for me attached explanation text obviously means tooltips instead of regular texts.
Or texts in combo's list, but cut of from lineEdit when selecting an item(as fit to window in zoom combobox).

wysota
11th December 2007, 08:08
Can I have a QComboBox with a validator with invalid input and still have it editable?
Why not?


The above will not function correctly due to the validator but I must maintain the ability to let the user enter a number that might not be present in the combobox.

Any suggestions?

1. Don't use combobox, but a completer as suggested

or

2. Write a custom delegate for your combobox's listview where you will display not only the main data but also the explanation text. The item's "text" will still only be a number, but something else will be displayed.

coderbob
11th December 2007, 16:44
Thank you for the suggestions.

Wysota,



Quote:
Originally Posted by coderbob View Post
Can I have a QComboBox with a validator with invalid input and still have it editable?

Originally Posted by Wysota
Why not?

I do not recall the exact source but the Trolls say some where that it is a bad idea and in the code I attempted it causes random results.

I was using a completer along with the combo box but to use just a completer still leaves me with the problem of invalid input due to explanation text next to the selections. I Would use tooltips but that takes an extra step to view, might not always be apparent to end users, and over all just makes things more complicated for the end user.

A custom delegate might be the best way to go on this.

Thanks again for the help

Bob

EDIT:
I thought (hoped) that invalid input from a combo box would just be dropped and it would leave me with the data I needed but still provide the extra information.

mchara
12th December 2007, 06:38
hmm, so way not connecting current item changed and cut explanation texts visible in combo's list to keep lineEdit numerical only.
You would have explanations visible while selecting items from list, but validator would accept only numbers.