两种方式
假设有 QGraphicsItem item;
方式一
QPixmap pix(item->boundingRect().width(),item->boundingRect().height());
QPainter painter;
pix.fill(Qt::transparent);//用透明色填充
painter.begin(&pix);
item->paint(&painter,nullptr,nullptr);
painter.end();
pix.save("D:/111111ww.png");
方式二
QSvgGenerator generator;
generator.setFileName("D:/2223323233.svg");
generator.setSize(QSize(200, 200));
generator.setViewBox(QRect(0, 0, 200, 200));
generator.setTitle(("SVG Generator Example Drawing"));
generator.setDescription(("An SVG drawing created by the SVG Generator "
"Example provided with Qt."));
//![configure SVG generator]
//![begin painting]
QPainter painter;
painter.begin(&generator);
//![begin painting]
// QStyleOptionGraphicsItem *styleItem =new QStyleOptionGraphicsItem();
item->paint(&painter,nullptr,nullptr);
//![end painting]
painter.end();
本文介绍了两种将QGraphicsItem渲染为图片的方法:一种是通过QPixmap保存为PNG格式;另一种是使用QSvgGenerator生成SVG文件。这两种方法都涉及到了QPainter的使用,并展示了如何设置画布及保存文件。
3790

被折叠的 条评论
为什么被折叠?



