为什么 Qt 不断打印“couldn't create image from”?对于 SVG?
我想在 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 库中的调用。
所以我想我的问题实际上有两个:
- 如标题所示,为什么 Qt 即使在正确渲染 SVG 时也会打印这个?
- 在不重新编译 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:
- As in the title, why is Qt printing this even when rendering SVGs correctly?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
#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?
回复 #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 toQString.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?我也遇到过类似的问题,我使用 Adobe Illustrator 创建图像,然后另存为 SVG。问题是 Illustrator 中的隐藏图层仍然会写入 SVG 文件,但其显示属性设置为“无”。就我而言,我“放置”的文件的隐藏层 - 以了解我正在创建的背景是否看起来不错。
这会导致您的图像按照您所说的那样正确渲染,但 Qt 抱怨 SVG 文件中缺少不可见层。如果您在文本查看器中打开 SVG 文件,您可能会发现隐藏层看起来像这样:
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:
Qt will complain about the missing "../logo_preview.png" file.
Hope this helps