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

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

本文关键字:类型 定义 QWidget QGraphicsItem 聚合 QPixmap      更新时间:2023-10-16

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

我尝试过,基于这个:

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

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

我认为

另一种方法是在项目矩形上创建另一个 QGraphicsView,然后渲染它,但我认为这会占用太多的开销......

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