Qt如何访问资源
Qt Creator 提供了将一些资源附加到项目的可能性。 我在项目目录中创建了一个名为:Images 的文件夹。在里面我有我创建的文件splash1.jpg,
然后创建了资源文件并附加了该文件,如下图所示:
现在从代码访问此类文件的正确方法是什么? 我尝试过
QPixmap pixmap( "Images/splash1.jpg" );
QPixmap pixmap( "./Images/splash1.jpg" );
,但没有一个有效。
如果我只放 ./Images/splash1.jpg 至少在我使用 qmake 和 make 手动编译时可以工作,因为在运行时有正确的路径,但无法使其在 qt 创建者中工作
有什么想法吗?
干杯,
Qt Creator give the possibility to attach some resource to the project.
I created in the project directory a folder called: Images. inside i have the file splash1.jpg
I created then the Resources file and attached the file as shown in the following figure:
Which is now the correct way to access from code such a file?
I tryed
QPixmap pixmap( "Images/splash1.jpg" );
QPixmap pixmap( "./Images/splash1.jpg" );
but none of them worked.
if i put just ./Images/splash1.jpg work at least if i compile by hand with qmake and make because has the correct path at runtime but no way to make it work within qt creator
Any idea??
Cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Qt5 资源 解释了您所需的一切。您必须将冒号放在源树中的路径之前。您还放置了一个前缀,例如
:/Images/Images/splash1.jpg
。这会给你一条路。如果您需要 URL,请使用 qrc: 方案。
Qt5 resources explains all you need. You have to put the colon before the path in the source tree. You also have placed a prefix, so
:/Images/Images/splash1.jpg
. This will give you a path.If you need a URL, then use the qrc: scheme.
加载Qt资源的正确方法是:
:/Images/Images/splash1.jpg
。您还可以做的是为资源分配一个别名。这样你就不需要 .jpg:
:/Images/splash
The correct way to load with Qt resources is:
:/Images/Images/splash1.jpg
.What you could also do, is assign an alias to the resource. This way you don't need the .jpg:
:/Images/splash
您可以使用“:/前缀/图像名称”。
You can use ":/prefix/name of your image".