How to set QComboBox text color using setStyleSheet?
-
Hello,
I would like to change text color in QComboBox like this:


( I only want red text "Text abcd…." like in the picture). I know that I can use QLineEdit, but I would like to use setStyleSheet
-
@jsulm Thank you, but I saw this document. I don't see any example to QComboBox which I can use. But of course I can be wrong.
-
@JonB I try:
ui->comboBox->setStyleSheet("QLineEdit {color: red}"); ui->comboBox->setStyleSheet("QListView::item:selected {background:red}");and many other but with no good result.
@TomNow99 said in How to set QComboBox text color using setStyleSheet?:
ui->comboBox->setStyleSheet("QListView::item:selected {background:red}");
Why do you use
QListViewhere? The link states:The pop-up of the
QComboBoxis aQAbstractItemViewand is styled using the descendant selector:QComboBox QAbstractItemView { border: 2px solid darkgray; selection-background-color: lightgray; }so why not read that and try that example?
-
@JonB I try this:
QComboBox QAbstractItemView { border: 2px solid darkgray; selection-background-color: lightgray; }But this is not what I want. I don't want change items colors but only header color.
-
@JonB I try many options. I would like that what I write in first post and add 2 example images. I only want change header text in QComboBox.
-
@JonB When I do this my app crash because lineEdit() return QWidget(0x0).
I read that only editable QComboboxes have lineEdites()
@TomNow99
That will happen if you have not doneui->comboBox->setEditable(true), because it won't have a line edit. But you must have done that, because you want your combobox to be editable so that it has a line to type into, mustn't you....? Please take the time to read https://doc.qt.io/qt-5/qcombobox.html#lineEdit if you want to start usinglineEdit().I find you can also do
ui->comboBox->lineEdit().setObjectName("combobox"); Stylesheet: #combobox {color: green;}if you prefer the ability to do it that way. I think you cannot access the
lineEdit()from QSS just viaQComboBox QLineEdit { ... }, for whatever reason.