Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt在“中正确呈现此SVG”;“调试”;模式,但不在“中”;“释放”;_C++_Qt_Svg - Fatal编程技术网

C++ Qt在“中正确呈现此SVG”;“调试”;模式,但不在“中”;“释放”;

C++ Qt在“中正确呈现此SVG”;“调试”;模式,但不在“中”;“释放”;,c++,qt,svg,C++,Qt,Svg,我遇到了这个奇怪的问题,当我针对调试DLL(Qt5.12.2,开源)构建调试和链接时,我得到了预期的渲染效果 当我为release和针对release DLL的链接构建时,图像是完全空白的。该程序是从MSVC运行的,因此应该正确设置dll路径。有人知道发生了什么吗 #include <QApplication> #include <QSvgRenderer> #include <QPainter> int main(int argc, char *argv[]

我遇到了这个奇怪的问题,当我针对调试DLL(Qt5.12.2,开源)构建调试和链接时,我得到了预期的渲染效果

当我为release和针对release DLL的链接构建时,图像是完全空白的。该程序是从MSVC运行的,因此应该正确设置dll路径。有人知道发生了什么吗

#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;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
//https://commons.wikimedia.org/wiki/File:USAF-1951.svg
QSVGrender渲染器(QString(“USAF-1951.svg”);
QImage图像(512,512,QImage::Format_Grayscale8);
QPainter painter(画师和图像);
renderer.render(&painter);
image.save(“USAF-1951.bmp”);
返回0;
}

我尝试了一些其他SVG图像,它们似乎很有效。不确定这张图片出了什么问题。

显然,如果我在调试和发行版中都按照预期在works中设置了背景

#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;
}
#包括
#包括
#包括
//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
质量保证申请a(argc、argv);
QSVGrender渲染器(QString(“USAF-1951.svg”);
QImage图像(512,512,QImage::Format_Grayscale8);

image.fill(255);//OP在his中提供了正确的修复,但没有解释为什么需要这样做。因此,我将介入:

Qt doc.about
QImage::QImage()

构造具有给定宽度、高度和格式的图像

如果无法分配内存,将返回空映像

警告:这将使用未初始化的数据创建QImage。在使用QPainter绘制图像之前,请调用以使用适当的像素值填充图像。

(强调我的观点。)

未初始化意味着图像像素字节中可能有任何值。如果都是
0
s,则alpha值也将是
0
s。这可能解释了为什么什么都没有出现

现在,另一个注意事项是为什么这可能在调试模式下工作:

OP明确提到了MSVC。微软的人试图为调试提供最好的支持,并决定用模式填充每个分配的内存(在调试模式下),例如
CD
代表“已分配但尚未初始化”。(这里有更多信息:)

有时,它真的很有用。将此位模式解释为
float
double
会产生非常奇怪的数字(通过一点位经验很容易识别),十六进制视图中的整数值变得非常明显

但是,这有一些缺点:未初始化的
bool
在调试模式下(以某种方式)将始终“初始化”为
true
,在调试模式下,它在发布时具有任意值。→ 可以想象的最糟糕的事故:调试运行,但发布偶尔失败。(我最害怕的bug。)

在OP的例子中,它(可能)是相似的:在调试模式下,图像总是有一个浅灰色背景,不透明度高到足以忽略发布模式下的意外透明度…见上文。(或者,OP可能会得到一个类似于过去午夜后从电视上知道的噪音模式。不确定这是否有进一步的帮助…)