Hi,

I need to create an QImage from svg file. How do I keep the ration? Also how do I get all element ids in the svg file?

Here is the code to start. Many thanks.

Qt Code:
  1. #include <QApplication>
  2. #include <QSvgRenderer>
  3. #include <QPainter>
  4. #include <QImage>
  5. #include <QDebug>
  6.  
  7. // In your .pro file:
  8. // QT += svg
  9.  
  10. int main(int argc, char **argv)
  11. {
  12. // A QApplication instance is necessary if fonts are used in the SVG
  13. QApplication app(argc, argv);
  14.  
  15. // Load your SVG
  16. QSvgRenderer renderer(QString("./TW_CHW194.svg"));
  17.  
  18. qDebug() << "size =" << renderer.defaultSize();
  19.  
  20. // Prepare a QImage with desired characteritisc
  21. QImage image(100, 500, QImage::Format_ARGB32);
  22. image.fill(0xaaA08080); // partly transparent red-ish background
  23.  
  24. // Get QPainter that paints to the image
  25. QPainter painter(&image);
  26. renderer.render(&painter);
  27.  
  28. // Save, image format based on file extension
  29. image.save("./svg-2-image.png");
  30. }
To copy to clipboard, switch view to plain text mode