Hi, I am working on a project where the user sees some stuff (its a graph editor, with Qt and QML) and I want him to be able to export what he is seeing to an image. So, I have first tried with QPixmap and I can export the image very well: the content of the image is exactly the same as in the software. But I would like to do it in svg because since graphs can be quite big, I want the user to have an image where he can zoom without quality loss.
Anyway, the code is nearly the same, but somehow… It just doesn’t give the same image: when I export the image to .png with QPixmap, the sizes of all the elements are ok, but in SVG the size of some elements are bigger thant they should.
What I want to drow ar QDeclarativeItems.
Here is the painting code:
//PNG OR OTHER FORMAT
QDeclarativeItem* item = parent->rootObject()->findChild<QDeclarativeItem*>("treeBase");
QPixmap pix(item->width(),item->height());
QPainter painter(&pix);
QStyleOptionGraphicsItem options;
paintEverything(item, &painter, &options);
QPixmap result = pix.copy(minx,miny,maxx-minx,maxy-miny);
result.save(fileName);
//SVG DOES NOT WORK WELL.
/*
QSvgGenerator generator;
generator.setFileName(fileName);
generator.setSize(QSize(maxx-minx,maxy-miny));
generator.setViewBox(QRect(minx, miny,maxx-minx,maxy-miny));
generator.setTitle("title");
generator.setDescription("desc");
QPainter painter;
painter.begin(&generator);
QStyleOptionGraphicsItem options;
QDeclarativeItem* item = parent->rootObject()->findChild<QDeclarativeItem*>("treeBase");
paintEverything(item, &painter,&options);
painter.end();
*/
minx, miny maxx and maxy.. are just ints.
So, here the code looks the same, except for the painterbegin() and painter.end(). I have seen on the Qt doc that for SVG this is needed so…
Now, the paintEverything function:
void ExportImage::paintEverything(QGraphicsItem* item ,QPainter* painter,
QStyleOptionGraphicsItem* options){
QRectF toDraw = item->mapRectToScene(item->boundingRect());
painter->translate(toDraw.x(),toDraw.y());
if(item->isVisible())
item->paint(painter, options, NULL);
painter->translate(-toDraw.x(),-toDraw.y());
for (it = children.begin() ; it != children.end(); ++it){
paintEverything(*it,painter,options);
}
}
The map to scene just geves me the coord coordenates.
I don’t see anything else very special.
I can also give you the paint function for my classes inheritent from QDeclarativeItem, but the errror cannot come fgrom there because it works por QPixmap:
void Node::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget){
QPen pen(QColor("grey"), 1);
painter->setPen(pen);
painter->setBrush(QBrush(color(), Qt::SolidPattern));
painter->setRenderHints(QPainter::Antialiasing, true);
painter->drawEllipse(boundingRect());
}
as you see.. nothing very special.
So, does any one have any idea, because I just don’t understang how it could work with QPixmap and not with QtSVG…
Thank you a lot!
↧