4

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?

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • try QComboBox:!editable:focus – Rachel Gallen Feb 08 '13 at 19:25
  • Great question! I was able to get somewhere with `setStyleSheet("QLineEdit, QComboBox { background-color: green; }" "QComboBox:focus, QComboBox:focus QListView { background-color: red; }");` but it seems that I am missing something... – Nemanja Boric Feb 08 '13 at 19:52
  • Even setting this works, but not the background `setStyleSheet("QLineEdit, QComboBox { background-color: green; }" "QComboBox:focus, QComboBox:focus QAbstractItemView { background-color: red; selection-background-color: red}");` – Nemanja Boric Feb 08 '13 at 19:56
  • @RachelGallen I already tried it before asking here. –  Feb 09 '13 at 01:16
  • @NemanjaBoric: Yes, I tried it too, but it changes only the background color of selected text. Also, when I use `QComboBox { background-color: transparent; }` my `:focus` starts to work, but I lose my out-of-focus background. Putting `padding: 2` gives some additional information - there is somthing in front of the combo box. It should be `QLineEdit`, but it's not, or not exactly. I tried `QComboBox > QLineEdit` without success. –  Feb 09 '13 at 01:34

1 Answers1

0

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.

piccy
  • 336
  • 2
  • 12
  • Your are probably right, but it changes the code of the program. My question was not quite correct. I already have such kind of solution, and it is simpliest: to put the `QComboBox` inside a `QFrame`, with apropriate background and set `QComboBox { background-color: transparent; }`. I will clarify my question. –  Feb 09 '13 at 01:59