4

I found this example on the web on how to implement custom properties accessible from QSS for custom QWidgets: https://wiki.qt.io/Qt_Style_Sheets_and_Custom_Painting_Example

Does anyone know how can I implement the widget so that I can have different colors for hover or pressed states?

Current stylesheet looks like this:

SWidget
{
 qproperty-lineColor: yellow;
 qproperty-rectColor: red;
}

I want to be able to have something like this:

SWidget:hover
{
 qproperty-lineColor: blue;
 qproperty-rectColor: green;
}

SWidget:pressed
{
 qproperty-lineColor: orange;
 qproperty-rectColor: violet;
}

Note: I know it is possible to implement mouse events and change the colors using qproperties specific to the mouse events, for example:

SWidget
{
 qproperty-lineColor: yellow;
 qproperty-rectColor: red;
 qproperty-lineColor-hover: orange;
 qproperty-rectColor-hover: violet;
}

but I would like to be able to make it work using the original qss/css way.

Regards!

Pamputt
  • 173
  • 1
  • 11
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
  • Hi, do you really want to use the qproperty system ? Because just hover/pressed change in your design can be done with pure CSS. – Martin Sep 21 '14 at 09:18
  • yes, :hover and :pressed are triggered fine for SWidget widget but only for standard properties (color, background-color, border, image, etc.). However, I need custom properties. For example I have a drawing of a few lines that I need to change in a different color on hover and I would like it done with QSS. This way widgets are way more easier to customize. – Jacob Krieg Sep 21 '14 at 09:43

1 Answers1

0

When user hover or pressed widget, you should change some widget property. And make different QSS selector for that properties. After change property you should make unpolish\polish of application styles.

qApp->style()->unpolish( this );
qApp->style()->polish( this );

Where "this" current window pointer. That "magic" code will help to have affect of appropriate QSS selector.

stanislav888
  • 374
  • 2
  • 9