Qt在“调试"窗口中正确地呈现该SVG.模式,但不处于“释放"状态 [英] Qt renders this SVG correctly in "debug" mode but not in "release"

查看:153
本文介绍了Qt在“调试"窗口中正确地呈现该SVG.模式,但不处于“释放"状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个奇怪的问题,当我进行调试并针对调试dll(Qt 5.12.2,开放源代码)进行链接时,我得到了预期的渲染.

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.

当我为发布而构建并针对发布dll进行链接时,该映像完全空白.该程序是从MSVC运行的,因此应正确设置dll路径.有人知道怎么回事吗?

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;
}

我尝试了其他一些SVG图像,但它们似乎可以正常工作.不知道这张图片怎么了.

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

推荐答案

OP在他的自我解答中提供了正确的解决方法,但是错过了解释为什么这样做是必要的.因此,我将介入:

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

Qt文档.关于QImage::QImage():

QImage :: QImage(整数宽度,整数高度,QImage: :Format格式)

以给定的宽度,高度和格式构造图像.

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.

警告:这将创建带有未初始化数据的QImage.调用 fill()之前,先用适当的像素值填充图像用QPainter在其上绘制.

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.

(强调我的.)

未初始化表示图像像素字节中可能有任何值.如果全部为0,则alpha值也将为0.那也许可以解释为什么什么都没出现.

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明确提到了MSVC. MS专家们试图为调试提供最好的支持,并决定使用诸如的模式填充每个分配的内存(在调试模式下). CD代表已分配但尚未初始化". (有关此内容的更多信息: SO:在Visual Studio C ++中,什么是内存分配表示形式?.)

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?.)

有时候,这确实很有帮助.将此位模式解释为floatdouble会产生非常奇怪的数字(通过一点点经验即可轻松识别),并且十六进制视图中的整数值变得非常明显.

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.

但是,这有一些缺点:未初始化的bool将始终被初始化".到true的调试模式(以某种方式),其中在发行版中具有任意值. →可以想象的最严重的事故:调试运行,但偶尔会释放失败. (我最害怕的错误.)

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.)

在OP的情况下,它(可能)是相似的:在调试模式下,图像始终具有浅灰色背景,并且具有不透明度,该透明度足以在释放模式下忽略意外的透明度...参见上文. (或者,OP可能会像过去午夜过后那样从电视上获得噪声模式.不确定是否进一步帮助了……)

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...)

这篇关于Qt在“调试"窗口中正确地呈现该SVG.模式,但不处于“释放"状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆