Question

I have an editable combo box on the input form, which background must change when it receives focus. The following code works perfect for QLineEdit but has no effect on QComboBox.

QLineEdit, QComboBox { background-color: green; }
QLineEdit:focus, QComboBox:focus { background-color: red; }

Is it possible to make QComboBox behaves as expected like QLineEdit using only Qt style sheets?

Was it helpful?

Solution

You may need to do this by subclassing QLineEdit, and installing it into the combo box (with QComboBox::setLineEdit()). Then, override the focusIn() and focusOut() functions of QLineEdit, and set a style sheet with the appropriate background color in those functions.

Another way would be to install an event handler on the combo box, (and/or its associated QLineEdit) and trap focus in/out events, and change the style sheet then.

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