为什么 Qt 不断打印“couldn't create image from”?对于 SVG?

发布于 2024-11-05 09:43:09 字数 900 浏览 1 评论 0原文

我想在 Qt-4.7 应用程序周围的菜单和按钮中使用 SVG。这些图像在 Linux 和 Windows 平台上正确呈现,但是一条令人讨厌的消息显示...

couldn't create image from ""

...似乎只要这些图像之一就打印到控制台已加载或更改状态(例如突出显示或禁用其容器小部件)。在应用程序运行的过程中,会打印许多这样的行,从而在合理的应用程序输出周围留下大量无意义的输出。

稍微浏览一下 Qt 代码,这似乎来自 svg/qsvghandler.cpp:2680 ,其中该行包含以下内容。

qDebug()<<"couldn't create image from "<<filename;

qDebug 的文档中,您将我认为我可以通过在编译时定义 QT_NO_DEBUG_OUTPUT 来阻止这一点,但这只会阻止应用程序的已编译调试调用,而不是 Qt 的 SVG 库中的调用。

所以我想我的问题实际上有两个:

  1. 如标题所示,为什么 Qt 即使在正确渲染 SVG 时也会打印这个?
  2. 在不重新编译 Qt 或其 SVG 库的情况下,如何防止 Qt 打印此内容并破坏应用程序的输出?

我还在 QtCentre 论坛

I would like to use SVGs in menus and buttons around a Qt-4.7 application. The images are rendered correctly across linux and windows platforms, however an obnoxious message reading...

couldn't create image from ""

...is printed to the console seemingly as soon as one of these images is loaded or changes state (like highlighting or disabling its container widget). Over the course of the run of the application, many of these lines are printed, leaving a lot of senseless output crufted around reasonable application output.

Poking around the Qt code a bit, this appears to be coming from svg/qsvghandler.cpp:2680 where the line contains the following.

qDebug()<<"couldn't create image from "<<filename;

From the documentation for qDebug, you would think that I could block this by defining QT_NO_DEBUG_OUTPUT at compiletime, but this will only block the application's compiled debug calls, not the one in Qt's SVG library.

So I guess my question is actually two-fold:

  1. As in the title, why is Qt printing this even when rendering SVGs correctly?
  2. Without recompiling Qt or its SVG library, how do I prevent Qt from printing this and crufting the application's output?

I've also posted this question on the QtCentre forums.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜还是长夜 2024-11-12 09:43:09

#2:您可以通过使用 qIntallMsgHandler 并编写一个仅丢弃消息的处理程序来阻止 qDebug 输出。另请参阅此问题:如何重定向 qDebug、qWarning、qCritical 等输出?

To #2: You can prevent qDebug output by using qIntallMsgHandler and writing a handler that just discard the message. Also see this question: How to redirect qDebug, qWarning, qCritical etc output?

无名指的心愿 2024-11-12 09:43:09

回复 #1:我不是 SVG 专家,但从出现该行的 Qt 源代码来看,Qt 似乎正在尝试加载 xlink:href 属性中引用的图像文件。您看到的输出似乎表明该属性的值是“”,或者该值是一堆用“”括起来的空白字符,通过调用QString.trimmed() 然后用作要加载到 QImage 中的图像文件的名称。我不太清楚文件名是否以空字符串或两个引号结尾:)

我不得不猜测您拥有的 SVG 文件是否填充了空字符串 xlink:href 属性 (可能是纹理图像的链接或文件名)或者 SVG 文件存在某些问题,导致 Qt 错误地解析它。我倾向于前者。

您断定它们渲染正确的标准是什么?也许您可以在文本编辑器中打开 SVG 文件并查看其中出现的 xlink:href 内容?

Re #1: I'm no expert on SVG but from the Qt source code where that line appears, it looks like Qt is trying to load an image file referenced in an xlink:href attribute. The output you see would seem to indicate that either the value for that attribute is "" or the value is a bunch of white space characters enclosed in "" which is removed by a call to QString.trimmed() which is then used as the name of an image file to be loaded into a QImage. I can't quite discern if the filename is ending up as an empty string or two quotes :)

I would have to guess that either the SVG files you have are filled with empty string xlink:href attributes (probably links or filenames for texture images) or there is something about the SVG file causing Qt to incorrectly parse it. I would lean towards the former.

What is your criteria for concluding that they are being rendered correctly? Perhaps you could open the SVG files in a text editor and look at the xlink:href occurances in it?

以歌曲疗慰 2024-11-12 09:43:09

我也遇到过类似的问题,我使用 Adob​​e Illustrator 创建图像,然后另存为 SVG。问题是 Illustrator 中的隐藏图层仍然会写入 SVG 文件,但其显示属性设置为“无”。就我而言,我“放置”的文件的隐藏层 - 以了解我正在创建的背景是否看起来不错。

这会导致您的图像按照您所说的那样正确渲染,但 Qt 抱怨 SVG 文件中缺少不可见层。如果您在文本查看器中打开 SVG 文件,您可能会发现隐藏层看起来像这样:

 <g id="bglogo_x5F_splash" style="display:none;">
    <image style="display:inline;overflow:visible;" width="1740" height="438" xlink:href="../logo_preview.png"  transform="matrix(1 0 0 1 154 409)">
    </image>
</g>

Qt 会抱怨缺少“../logo_preview.png”文件。

希望这有帮助

I've had a similar issue, I used Adobe Illustrator to create images and then save as SVG. The problem is that hidden layers in Illustrator still gets written to the SVG file, but its display property is set to "none". In my case a hidden layer of a file I "placed" - to get an idea if the background I'm creating would look OK.

This causes your image to render correctly as you say, but Qt complains about the missing invisible layer in the SVG file. If you open the SVG file in a text viewer you might find the hidden layer looks something like this:

 <g id="bglogo_x5F_splash" style="display:none;">
    <image style="display:inline;overflow:visible;" width="1740" height="438" xlink:href="../logo_preview.png"  transform="matrix(1 0 0 1 154 409)">
    </image>
</g>

Qt will complain about the missing "../logo_preview.png" file.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文