Solved Why setting background color of QLineEdit has no effect?
I followed this guide and tried to change the background of QLineEdit object, but there is no change whatsoever. This is my code that should make the background color light green.
QLineEdit *unit_search = new QLineEdit(catalog_units); QPalette pal(unit_search->palette()); pal.setColor(QPalette::Background, QColor(220, 255, 220)); unit_search->setAutoFillBackground(true); unit_search->setPalette(pal);
I am using Qt Creator 3.6.0 based on Qt 5.5.1.
The only difference with my other project that has working background colors is that this project has no design forms. Native events also do not work with this project, even 100 % identical code to another project in where they work.
palette.setColor(QPalette::Base,QColor(220, 255, 220));
It seems LineEdit uses that and not QPalette::Background.
@mrjj Thanks. It works.
Just a side note: if you want this for all your lineEdits then
a style sheet might be easier. Like in having this color all the time.
But if to use for ok / invalid etc or something like that, then palette is fine.
I still can't change the background color of my QLineEdit. I do the same thing as you do and nothing happens.
It would then be good to list what platform(lin,win,mac) and what Qt version(Qt5.13,Qt6 etc) you are trying this on as things change in 7 years :)