查看完整版本: [-- QGraphicsItem 如何导出为png或者svg图? --]

QTCN开发网 -> Qt基础编程 -> QGraphicsItem 如何导出为png或者svg图? [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

shixingya 2017-11-24 18:21

QGraphicsItem 如何导出为png或者svg图?

QGraphicsItem 如何导出为png或者svg图?

九重水 2017-11-28 14:16
这个根本不是问题好不?你打开SVG那个module一看就知道了。

shixingya 2017-12-01 20:01
九重水:这个根本不是问题好不?你打开SVG那个module一看就知道了。[表情] (2017-11-28 14:16)

非常感谢 终于解决了

shixingya 2017-12-01 20:31
两种方式
假设有 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();

九重水 2017-12-02 09:10
结贴是个好习惯。

shixingya 2017-12-03 00:57
问题又来了,我将多个图元选中,用QGraphicsItemGroup组合它们,想导出png图,还是没有好的思路,求众仙指点

shixingya 2017-12-04 12:04
用了一个不太好的方式

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


查看完整版本: [-- QGraphicsItem 如何导出为png或者svg图? --] [-- top --]



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