Question

I need a widget from QComboBox, user can select item from popup or input directly in the linedit, but how to prevent the inputs out of range of the items in QComboBox? or can the text input in the lineedit always be legal?
Thanks

Was it helpful?

Solution

Refer to the QComboBox::setValidator(const QValidator*) member function:

http://qt-project.org/doc/qt-4.8/qcombobox.html#setValidator

This allows you to set a validator to constrain any input that users type into the line edit of the combo box.

Qt provides three validators: QIntValidator to constrain inputs to integers within a certain range; QDoubleValidator to constrain inputs to floating point values within a certain range and with a specified precision; and QRegExpValidator to constrain inputs to a particular regular expression. If your validation needs are more complex, you may also subclass QValidator and write your own and then install it into the combo box. See also: http://qt-project.org/doc/qt-4.8/qvalidator.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top