查看完整版本: [-- 我将多个图元选中,用QGraphicsItemGroup组合它们,想导出png图,还是没有好的思路,求众仙指点 --]

QTCN开发网 -> Qt基础编程 -> 我将多个图元选中,用QGraphicsItemGroup组合它们,想导出png图,还是没有好的思路,求众仙指点 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

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);
}

shixingya 2017-12-04 12:47
上面的方式 虽然可以曲线救国,但是相信有更好的方式 求推荐


查看完整版本: [-- 我将多个图元选中,用QGraphicsItemGroup组合它们,想导出png图,还是没有好的思路,求众仙指点 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled