Using QtSVG and QPixmap does not give the same result



  • 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!



  • Would you have some screenshots?
    I mean, what kind of elements look different. Is it mostly text? Did you happen to use point sizes or pixelsizes for text?



  • Hi, thank you for your answer.

    Here are two pictures.(I converted both to png with a software because I did'nt find any webside where i could upload svg images):
    This is the one created with QPixmap:
    http://imageshack.us/a/img5/9455/totof.png
    and this is the one with QSVGGenerator:
    http://imageshack.us/a/img22/5808/descargap.png

    as you can see, the text are actually the only thing that is exactily correct on both images. The problem are the circles: they become bigger (and thus, the image bicomes bigger. Text is moved to the left because it's actually on the good coordonates, if the circles were littler the text would be in the center and it would be just perfect.

    thank you again!

    --- edit:

    I just found a "half solution":
    by commenting this line: //generator.setSize(QSize(maxx-minx,maxy-miny));
    the size of the nodes and lines is now correct.
    But now, the size of the text isn't.
    It's juts more little than it should. Any idea? Thank you!



  • Just wondering if you realize that SVG is scalable, so your svg viewer can zoom in/out and avoid many of the problems you are seeing.
    To me it looks like Qt is doing the right thing, the ratios all look proper and correct. The svg one just looks like its designed for 96dpi and viewed at 150 dpi (or so).

    I would say that there is no problem. Sorry to be not very helpful :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.