ceraolo
4th January 2014, 11:20
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.
#include "mainwindow.h"
#include <QPainter>
#include <QSvgGenerator>
#include <QMainWindow>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *){
int xPos;
QPainter p;
QRect r;
QString s="This is some significant text";
//Writing on MainWindow:
p.begin(this);
p.drawText(0,0,0,0,0,s,&r);
p.drawText(r,s);
p.drawRect(r);
p.drawText(0,40,s);
QFontMetrics m=p.fontMetrics();
xPos=m.width(s);
p.drawLine(QPoint(xPos,40),QPoint(xPos,40-m.height()));
p.end();
//Creating and writing on the svg:
QSvgGenerator generator;
generator.setFileName("svgIssue.svg");
p.begin(&generator);
p.drawText(0,0,0,0,0,s,&r);
p.drawText(r,s);
p.drawRect(r);
p.drawText(0,40,s);
QFontMetrics m1=p.fontMetrics();
xPos=m1.width(s);
p.drawLine(QPoint(xPos,40),QPoint(xPos,40-m.height()));
p.end();
}
This is what I see on screen (i.e. it is correct)
9906
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:
9907
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
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.
#include "mainwindow.h"
#include <QPainter>
#include <QSvgGenerator>
#include <QMainWindow>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *){
int xPos;
QPainter p;
QRect r;
QString s="This is some significant text";
//Writing on MainWindow:
p.begin(this);
p.drawText(0,0,0,0,0,s,&r);
p.drawText(r,s);
p.drawRect(r);
p.drawText(0,40,s);
QFontMetrics m=p.fontMetrics();
xPos=m.width(s);
p.drawLine(QPoint(xPos,40),QPoint(xPos,40-m.height()));
p.end();
//Creating and writing on the svg:
QSvgGenerator generator;
generator.setFileName("svgIssue.svg");
p.begin(&generator);
p.drawText(0,0,0,0,0,s,&r);
p.drawText(r,s);
p.drawRect(r);
p.drawText(0,40,s);
QFontMetrics m1=p.fontMetrics();
xPos=m1.width(s);
p.drawLine(QPoint(xPos,40),QPoint(xPos,40-m.height()));
p.end();
}
This is what I see on screen (i.e. it is correct)
9906
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:
9907
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