Important: Please read the Qt Code of Conduct -

(Solved)The background-color of the QLineEdit

  • Hello, everyone.

    I met a problem that I could not set the background-color of the QLineEdit as well as I put the QLineEdit on a widget.
    and the widget is put on the QMainWindow.

    Both I set the style sheet"background:white" and "background:white", the color of the LineEidt is the widget's background-color.

    any suggestions? thanks.

    best regards.

  • Are you working on maemo? I had similar problem in my application ("this thread":, and it seems to be that this is a bug in Qt (I have not created the bug report in the bug tracker yet, as I have not had time to validate the issue and make it more specific).

  • To match all the QLineEdits, you must use the class selector:

  • or just:


    lineEdit->setStyleSheet("QLineEdit{background: black;}");

  • Thanks for all replies.

    I use the setStyleSheet funtion like
    lineEdit->setStyleSheet("QLineEdit{background: white;}");
    @ to set the background-color white;
    but it doesn't work.

    and I try
    QLineEdit {
    background: qradialgradient(cx:0, cy:0, radius: 1,
    fx:0.5, fy:0.5, stop:0 white, stop:1 rgba(0,190,0, 60%));
    border-radius: 9px;

    it worked.
    but the color is not what I need.

  • Which OS is this happening?

  • I test the app on Nokia device 5230. The OS is s60v5.

  • I just tested this on latest Qt 4.7.x and it works. When you set the background color as a stylesheet, the lineEdit will lose the themed graphics (including borders etc) as theme graphics are coming from native side and QS60Style cannot make changes to those. So it is either stylesheeted background, borders etc. or theme graphics.

    My code was:
    QString lineEditStyle("QLineEdit {background: white;}");
    QLineEdit *testLineEdit = new QLineEdit(this);

    In case you can't get white background visible for the line edit with that code, what Qt version you have?

  • It worked for me as well... are you trying to change the border-color? or the whole background?

  • I solved the problem from another aspect....It seems it is not the best way.

    as I first set the background-image of the widget on the QMainWindow, and put the widget() on the QMainWindow, the background-color of QLineEdit was disable.

    So I use QPainter to paint the image I use as the background-image on the widget, and then put the widget on the QMainWindow. and put the lineEdit's background-color white:
    lineEdit->setStyleSheet("QLineEdit{background: white;}");

    It worked.

Log in to reply