如何使用QGraphicsScene::setStyle()?

How to use QGraphicsScene::setStyle()?

我正在使用 QGraphicsProxyWidgetQWidget 嵌入到 QGraphicsScene 中。问题在于绘制这些小部件的样式。 来自 documentation:

A top-level item's style defaults to QGraphicsScene::style. A top-level widget's style defaults to QApplication::style

所以,我必须将样式设置为 QGraphicsScene。现在,我正在使用 QWidget::setStyleSheet 来设置基于 CSS (QSS) 的自定义样式。

我的问题是:如何使用 QGraphicsScene::setStyle,它需要 QStyle 的实例,因为我没有实例?有人可以 post 使用它并设置启用样式表的样式的示例吗?

我已经尝试过QStyleFactory::create,但是无法创建QStyleSheetStyle的实例。

由于您没有 QStyle 的实例,因此 QGraphicsScene 使用的默认样式是 QApplication::style(),如 documentation 中所述。

The scene's style defaults to QApplication::style(), and serves as the default for all QGraphicsWidget items in the scene.

这句话的第二部分暗示您可以在场景中自定义小部件(就像您现在使用 QWidget::setStyleSheet 所做的那样)。您可以在将小部件添加到场景之前为其指定样式 sheet,添加后它将保持其样式。

但是QGraphicsScene是不可能的,因为它没有这样的功能。 他们的样式应该使用 QPalette 定义并通过 QApplication::setPalette. Remember that QGraphicsScene also has functions like setBackgroundBrush and setForegroundBrush 设置。

QPalette pal;
pal.setColor(QPalette::Base, QColor(255, 0, 0));

QApplication::setPalette(pal);

QGraphicsScene* scene = new QGraphicsScene(400,400,400,400);
scene->setPalette(pal);

QGraphicsView* view = new QGraphicsView(scene);
view->show();