在 QPixmap 上渲染 QGraphicsItem:聚合 'QWidget w' 类型不完整,无法定义

Render QGraphicsItem on QPixmap: aggregate 'QWidget w' has incomplete type and cannot be defined

我想在不绘制场景的情况下将项目绘制到 QPixmap 中...

我试过了,基于this:

void Item::itemPaint(QPainter *painter)
{
    QStyleOptionGraphicsItem opt;
    QWidget w;
    paint(painter, &opt, &w);  // also tried NULL 
}

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
    ....
}


void caller()
{
    QPainter* painter;
    for(int i = 0; i< collectionView->scene()->items().size(); i++)
    {
        QGraphicsItem* qitem = collectionView->scene()->items().at(i);
        Item* item = new Item(*(dynamic_cast<Item*>(qitem)));
        QPixmap pItem(item->boundingRect().size().toSize());
        pItem.fill(QColor(Qt::color0).rgb());
        painter = new QPainter(&pItem);
        painter->setRenderHint(QPainter::Antialiasing);
        item->itemPaint(painter);
        painter->end();
    }
}

我明白了

error: aggregate 'QStyleOptionGraphicsItem opt' has incomplete type and cannot be defined
error: aggregate 'QWidget w' has incomplete type and cannot be defined

如何使用项目的 paint 方法将单个项目仅渲染到 QPixmap ?

我认为替代方案是在项目矩形上创建另一个 QGraphicsView,然后渲染它,但我认为这会产生太多开销...

#include <QWidget>#include <QStyleOptionGraphicsItem> 应该可以修复您遇到的编译错误。