Important: Please read the Qt Code of Conduct -

How to use QGraphicsScene::setStyle()?

  • Hi everyone,
    I am using QGraphicsProxyWidget to embed QWidgets inside a QGraphicsScene. The problem comes with the style with which this widgets are drawed. From the documentation :

    A top-level item's style defaults to QGraphicsScene::style. A top-level widget's style defaults to QApplication::style
    So, I have to set the style to QGraphicsScene. Right now, I am using QWidget::setStyleSheet to set custom styles based on CSS (QSS).

    My question is: how can I use QGraphicsScene::setStyle, which requires an instance of QStyle, since I haven't got one? Can somebody post an example using it and setting an stylesheet-enabled style?

    I have tried already QStyleFactory::create, but there is no way to create an instance of QStyleSheetStyle.

    Thank you very much,
    Best regards,

  • Moderators

    Since you are using QGraphicsProxyWidget you actually should set your stylesheet to your QApplication instance - using QApplication::setStylesheet()
    This is because widgets set to a QGraphicsProxyWidget are not allowed to have a parent set -> top level widget.
    Or - if not possible otherwise - to every widget itself.

  • Yes, ultimately I have used QWidget::setStyleSheet() to every widget inside the QGraphicsScene. I couldn't use QApplication::setStylesheet() because I have two sets of styles: the main interface style (which is applied to QApplication) and the user objects style, which was the one I wanted to use in QGraphicsScene.

    I was hoping that there would be an easier solution by means of QGraphicsScene::setStyle(), but seems to not be possible.

  • Moderators

    you could still combine both styles in one CSS. You just need to adapt your selectors.
    E.g. you could set an property to your widgets and only apply certain styles to them.