Ernest3.14
Ernest3.14

Reputation: 1012

Background color of styled QLineEdit flickers

When setting the background-color of a QLineEdit using stylesheets, there is a very noticeable flicker upon mouseover of the control. Example code:

QLineEdit* flicker = new QLineEdit(this);
flicker->setStyleSheet("QLineEdit {background: red;}");
flicker->show();

This only happens when running on Windows Vista and later, and not in XP. I think it has something to do with the default styling for Windows (Aero?) applications, because setting the style to QStyle::Fusion fixes the problem:

QLineEdit* flicker = new QLineEdit(this);
QStyle* fusion = QStyleFactory::create(QString("Fusion"));
flicker->setStyle(fusion);
flicker->setStyleSheet("QLineEdit {background: red;}");
flicker->show();

Edit: I also have an eventfilter set up such that the control gets repainted on mouseover, and the debugger is confirming that that gets called immediately.

Upvotes: 1

Views: 2170

Answers (2)

cleybertandre
cleybertandre

Reputation: 321

I've had a similar problem and resolved it by adding a border to QLineEdit, like this:

#dont_flick_lineedit{
    background-color: red;
    border: 1px solid #CCC;
}
#flick_lineedit{
    background-color: blue;
}

Upvotes: 1

Robert
Robert

Reputation: 807

Ran into the same problem and wanted to share a possible workaround:

The reason for the flickering of the QLineEdit on mouseover is probably that another style sheet is used for "QLineEdit:hover{...}" that still contains the defaults. Unfortunately, it does not seem to be enough to add "QLineEdit:hover{background-color: red}". The only way I found it to work correctly up until now is to use

flicker->setStyleSheet("QLineEdit{background-color: red;} QLineEdit:hover{border: 1px solid gray; background-color red;}");

Not quite sure why the border property needs to be set explicitly, but it worked for me.

Upvotes: 4

Related Questions