将 QPainter 渲染的结果存储到 QPixmap 实例中

Storing the results of QPainter rendering into a QPixmap instance

我想对由 GRadialGradient 生成并由 QPainter 渲染的渐变应用模糊效果。看起来像这样的图形效果我必须提供 pixmap and a QGraphicsScene and then call the ->render() method but I couldn't find any ways to add a QPainter directly into any subclass of QGraphicsItem

那么有什么办法吗?
我认为将 QPainter 渲染结果转换为 QPixmap 可以解决 problem.But 我不知道 how.And 我不知道转换然后应用模糊效果的性能如何实时会是。

以下是我目前所写内容的摘录:

void MainWindow::paintEvent(QPaintEvent *event){
    Q_UNUSED(event);

    QRadialGradient grad(QPoint(this->width()/2,this->height()/2) , 50);
    grad.setSpread(QGradient::RepeatSpread);
    grad.setColorAt(0 , QColor(0,0,0));
    grad.setColorAt(1 , QColor(100,100,100));
    QPainter paint(this);

    paint.setRenderHint(QPainter::Antialiasing , true);

    QRectF r1(0,0,this->width(),this->height());

    paint.drawRect(r1);
    QBrush brush(grad);
    paint.fillRect(r1 , brush);
...
...
}

结果如下:

谢谢。

你的问题显然是XY problem,而不是询问潜在的问题:How to display a QGraphicsItem that shows the circular gradient + blur effect, questions about an attempted solution: convert to QPixmap.

在这种情况下,最简单的方法是使用 QGraphicsRectItem,将渐变设置为画笔并将效果应用于该项目:

QGraphicsBlurEffect *effect = new QGraphicsBlurEffect(this);     
effect->setBlurRadius(100);

QRadialGradient grad(QPoint(50, 50), 50);
grad.setSpread(QGradient::RepeatSpread);
grad.setColorAt(0 , QColor(0, 0, 0));
grad.setColorAt(1 , QColor(100, 100, 100));

QGraphicsRectItem *item = new QGraphicsRectItem(0, 0, 100, 100); 
item->setBrush(QBrush(grad));
item->setGraphicsEffect(effect);