1

I got this weird problem, when I build for debug and link against debug dlls (Qt 5.12.2, open source) I get the expected rendering.

When I built for release and link against release dlls, the image is completely blank. The program is ran from MSVC so the dll paths should be setup correctly. Anybody know whats going on?

#include <QApplication>
#include <QSvgRenderer>
#include <QPainter>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //https://commons.wikimedia.org/wiki/File:USAF-1951.svg
    QSvgRenderer renderer(QString("USAF-1951.svg"));
    QImage image(512, 512, QImage::Format_Grayscale8);
    QPainter painter(&image);
    renderer.render(&painter);
    image.save("USAF-1951.bmp");
    return 0;
}

I tried a few other SVG images and they seem to work. Not sure whats up with this image.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Mikhail
  • 7,749
  • 11
  • 62
  • 136

2 Answers2

2

The OP provided the correct fix in his self-answer but missed to explain why this is necessary. So, I will step in:

Qt doc. about QImage::QImage():

QImage::QImage(int width, int height, QImage::Format format)

Constructs an image with the given width, height and format.

A null image will be returned if memory cannot be allocated.

Warning: This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter.

(Emphasize mine.)

Uninitialized means there might be any value in the image pixel bytes. If it were all 0s, the alpha values would be 0s as well. That might explain why there appeared nothing.

Now, an additional note why this may have worked in debug mode:

OP mentioned explictly MSVC. The MS guys tried to provide best support for debugging and decided to fill every allocated memory (in debug mode) with patterns like e.g. CD standing for "allocated but not yet initialized". (More about this here: SO: In Visual Studio C++, what are the memory allocation representations?.)

Sometimes, it is really helpful. Interpreting this bit-pattern as float or double yields quite strange numbers (easy to recognize with a little bit experience), and integral values in hex-view become quite obvious.

However, this has some draw-backs: An uninitialized bool will be always "initialized" to true in debug mode (somehow) where it has arbitrary values in release. → The worst imaginable accident: debug runs but release fails sporadically. (My most afraided bugs.)

In OP's case, it's (probably) similar: In debug mode, the image has always a light gray background with an opacity which is high enough to ignore the unexpected transparency while in release mode... see above. (Alternatively, the OP could've get a noise pattern as well like known from TV after midnight in the past. Not sure, whether this had helped further...)

Community
  • 1
  • 1
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
1

So apparently, if I set the background in works as expected in both debug and release.

#include <QApplication>
#include <QSvgRenderer>
#include <QPainter>
//https://stackoverflow.com/questions/55895293/qt-renders-this-svg-correctly-in-debug-mode-but-not-in-release
int main(int argc, char *argv[])
{
    //https://commons.wikimedia.org/wiki/File:USAF-1951.svg
    QApplication a(argc, argv);
    QSvgRenderer renderer(QString("USAF-1951.svg"));
    QImage image(512, 512, QImage::Format_Grayscale8);
    image.fill(255);//<- Need to set background
    QPainter painter(&image);
    renderer.render(&painter);
    image.save("Test.bmp");
    return 0;
}
Mikhail
  • 7,749
  • 11
  • 62
  • 136