I'm writing a program to create scientific plots on screen, and to produce Scalable Vector Graphics (SVG) copies on the hard disk.
I came across an issue on SVG's. To show it I've created the following code.
I know that to create the SVG in the paintEvent method is silly, but I just wanted to show the issue in a very compact code.

Qt Code:
  1. #include "mainwindow.h"
  2. #include <QPainter>
  3. #include <QSvgGenerator>
  4. #include <QMainWindow>
  5.  
  6. MainWindow::MainWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. {
  9. }
  10.  
  11. MainWindow::~MainWindow()
  12. {
  13.  
  14. }
  15. void MainWindow::paintEvent(QPaintEvent *){
  16. int xPos;
  17. QRect r;
  18. QString s="This is some significant text";
  19.  
  20. //Writing on MainWindow:
  21. p.begin(this);
  22. p.drawText(0,0,0,0,0,s,&r);
  23. p.drawText(r,s);
  24. p.drawRect(r);
  25.  
  26. p.drawText(0,40,s);
  27. QFontMetrics m=p.fontMetrics();
  28. xPos=m.width(s);
  29. p.drawLine(QPoint(xPos,40),QPoint(xPos,40-m.height()));
  30. p.end();
  31.  
  32. //Creating and writing on the svg:
  33. QSvgGenerator generator;
  34. generator.setFileName("svgIssue.svg");
  35. p.begin(&generator);
  36. p.drawText(0,0,0,0,0,s,&r);
  37. p.drawText(r,s);
  38. p.drawRect(r);
  39.  
  40. p.drawText(0,40,s);
  41. QFontMetrics m1=p.fontMetrics();
  42. xPos=m1.width(s);
  43. p.drawLine(QPoint(xPos,40),QPoint(xPos,40-m.height()));
  44. p.end();
  45. }
To copy to clipboard, switch view to plain text mode 


This is what I see on screen (i.e. it is correct)
winScreen.jpg

And this is what I see when I browse the svg file using google chrome (i.e. the right part of the rectangle and the vertical bar are misplaced:
svgToChrome.jpg

I.e. my software computes correctly the text width on screen, but DOES NOT inside the SVG.
You can imagine what such an error can cause on a program that creates scientific plots!

Can anyone suggest a workaround for this issue?
Thanks a lot in advance.
MC