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.


  • Qt Champions 2016

    @Tapsa said:
    Hi

    try this
    QPalette palette;
    palette.setColor(QPalette::Base,QColor(220, 255, 220));
    unit_search ->setPalette(palette);

    It seems LineEdit uses that and not QPalette::Background.



  • @mrjj Thanks. It works.


  • Qt Champions 2016

    @Tapsa
    super :)

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.