简体   繁体   English

样式为QLineEdit的背景色闪烁

[英]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. 使用样式表设置QLineEditbackground-color ,将鼠标悬停在控件上时会出现非常明显的闪烁。 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. 仅在Windows Vista和更高版本(而不是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: 我认为这与Windows(Aero?)应用程序的默认样式有关,因为将样式设置为QStyle::Fusion解决此问题:

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. 编辑:我还设置了一个eventfilter器,以便在鼠标悬停时重新绘制控件,并且调试器确认立即调用了该函数。

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. 鼠标悬停时QLineEdit闪烁的原因可能是“ QLineEdit:hover {...}”使用了另一个样式表,该样式表仍包含默认值。 Unfortunately, it does not seem to be enough to add "QLineEdit:hover{background-color: red}". 不幸的是,添加“ 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. 不太确定为什么需要显式设置border属性,但是它对我有用。

I've had a similar problem and resolved it by adding a border to QLineEdit, like this: 我遇到了类似的问题,并通过向QLineEdit添加边框来解决它,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM