shixingya |
2017-12-04 12:45 |
我将多个图元选中,用QGraphicsItemGroup组合它们,想导出png图,还是没有好的思路,求众仙指点
void PaintView::onAssemblageAction(bool) { QList<QGraphicsItem *> selectItems=this->scene()->selectedItems(); QGraphicsItemGroup * groupItems =this->scene()->createItemGroup(selectItems); QRectF rect =groupItems->boundingRect();
QGraphicsScene *scene =new QGraphicsScene(); scene->setSceneRect(rect); for(int i=0; i<selectItems.count(); i++) { QGraphicsItem *sp = qgraphicsitem_cast<QGraphicsItem*>(selectItems); scene->addItem(sp); } scene->clearSelection(); QImage image(rect.width(),rect.height(),QImage::Format_ARGB32); image.fill(Qt::transparent);//用透明色填充 QString pngName = "D:/scene44test.png"; QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing); scene->render(&painter); bool saveSuccess = image.save(pngName); for(int i=0; i<selectItems.count(); i++) { QGraphicsItem *sp = qgraphicsitem_cast<QGraphicsItem*>(selectItems); this->scene()->addItem(sp); } if (!saveSuccess) { QMessageBox::about(NULL,"","组合图元失败!"); } else QMessageBox::about(NULL,"","组合图元成功!");
SafeDelete(scene); } |
|