[Qt]How to support :pressed state of style sheet for custom widget?

蓝咒 提交于 2019-12-06 04:22:10

You can set a property to your QLabel (or whatever widget you are using) and change the value of that property. Then you use that property in your stylesheets.

Example:

this->setStyleSheet("*[myproperty=\"true\"] { background-color: red }");
d_label = new QLabel("dynamic label", this);
d_label->setProperty("myproperty", false);

Then in your mousePressEvent you set and in mouseReleaseEvent you unset that property:

d_label->setProperty("myproperty", true); // or false when you wish to unset it
style()->unpolish(d_label);
style()->polish(d_label); // force a stylesheet recomputation

You can do a "setStyleSheet()" in the mousePressEvent and mouseReleaseEvent with the style that you want ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!