ColinKennedy
ColinKennedy

Reputation: 998

PyQt - Change the color of a QLineEdit if it has no text

Is it possible to change a QLineEdit background color depending on if it has text written inside it, using just only its stylesheet?

This could be done using Python code, of course, but I was wondering if it could be done using a css property.

My ideal scenario:

CSS

QLineEdit {
    background-color: white;
}

QLineEdit:no-text-inside-it {
    background-color: red;
}

Quick side note - According to the answer to this SO post: Changing the Color of QLineEdit's Placeholder Text, the placeholder can't be modified directly so it looks like QLineEdit may not have this level of functionality. But I know that some adjustments to placeholders were made in Qt 4.7, so may be that's a way, now

That said, anyone know if what I'm looking for is possible?

Upvotes: 2

Views: 1743

Answers (1)

ingvar
ingvar

Reputation: 4377

Try this (works in PyQt5, should work in PyQt4 too):

QLineEdit
{
    background-color: black;
}

QLineEdit[text=""]
{
    background-color: red;
}

Upvotes: 3

Related Questions