嵌入可执行文件中的 Qt 图标

发布于 2024-08-10 08:05:20 字数 249 浏览 2 评论 0原文

我的选项卡小部件上有一些按钮。这些按钮顶部需要有一些图标。

我知道 QPixmap 允许我将图像放在按钮顶部,但我看到这些构造函数将文件路径作为参数。我想避免在构建后在文件中拖动图标。我想以某种方式将这些图标嵌入到可执行文件中,以减少为了使其工作而需要携带的包袱。

我怎样才能做到这一点?

我有兴趣了解如何实现我的目标,即不需要将图标文件与可执行文件一起拖动;如果您对我可能使用过的一些术语感到困惑,请关注这方面,因为我仍在学习 Qt。

I have some buttons on a tabwidget. These buttons need to have some icon on top of them.

I am aware of QPixmap that will allow me to put an image on top of a button, but I see that these constructors take a filepath as a parameter. I want to avoid dragging icons around in a file after I build. I would like to embed these icons in the executeable somehow, so as to reduce the baggage that I need to lug around in order to make it work.

How can I accomplish this?

I am interested in hearing ways to accomplish my goal of not needing to drag icon files around with the executable; please focus on this aspect if you are confused about some terminology I may have used, as I am still learning Qt.

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

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

发布评论

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

评论(3

沙沙粒小 2024-08-17 08:05:20

当然,您可以将图标、图像和所有其他自定义资源嵌入到您的应用程序中。
请阅读Qt 4 的资源系统(或对于 Qt 5,请点击此处

Of course you can embed icons, images and all other custom resources into your application.
Please read The Qt 4's Resource System (or click here for Qt 5)

甜中书 2024-08-17 08:05:20

您将有一个名为 ProjectName.qrc 之类的资源文件,其中列出了图标(通常为 PNG)。对于 Qt Creator,它已经为您提供了一个空白的 QRC 文件。指定“文件”时,您以“:/Images/MyCrazyIco​​n.png”的方式使用语法,并且图标是从嵌入到可执行文件中的资源加载的。请注意,Qt 并没有以最完美的方式管理资源,也就是说,它们可以按需加载,但并不总是占用系统内存;它们总是占用系统内存,如果它们很小的话,这确实不是什么大问题。对于稍后要刷新的大图形,只需指定一个真实的文件名(例如,“MyBigFile.png”而不是“:/MyBigFile.png”,后者指向可执行文件中的资源)。

对于资源语法,权力位于冒号字符“:”中,因此您甚至可以拥有“:MyFile.png”,前提是 QRC 文件与资源位于同一目录中。我就是这样做的,到目前为止,还没有看到这种语法的缺点。我讨厌在语法中包含“.../Images/...”,因此我的资源名称使用“:MyPic.png”进行寻址,而没有更长的“:/Images/MyPic.png”,并且一切正常。

Qt 文档指出,目前它们不支持 Windows 和 Mac 支持的真正资源处理,但将来情况可能会发生变化。请记住,“真正的资源支持”是指您可以在可执行文件中放置数兆字节的图形和声音,并且永远不用担心它们加载到内存中,直到您请求它们的神奇时间为止。加载可执行文件时,QRC 资源将加载到内存中。可加载文件支持的换肤功能足以弥补这一弱点。 Qt 正在迅速改进,有一天他们可以很好地支持 Windows 和 Mac 资源系统。我变得多余了,必须离开。塔塔。大家帮帮我吧,我需要积分。

You will have a resource file named something like ProjectName.qrc and these list the icons (usually PNGs). With Qt Creator it already starts out with a blank QRC file for you. When specifying the "file" you use the syntax in the manner of ":/Images/MyCrazyIcon.png" and the icon is loaded from the resource that is embedded into your executable. Note that Qt does not manage resources in the very most perfect way, which is to have them loadable upon demand but not always hogging up system memory; they ALWAYS hog up system memory, which really is not such a big deal if they are small. For the big graphics you want flushed out later, just specify a real file name (e.g., "MyBigFile.png" instead of ":/MyBigFile.png", which the latter points to a resource within the executable).

With the resource syntax the power is in the colon character ":", so you could even have ":MyFile.png", providing the QRC file is located in the same directory as the resources. That is how I am doing it, and so far see no downside of this syntax. I hate including ".../Images/..." in the syntax, so my resource names are addressed with ":MyPic.png" without the longer ":/Images/MyPic.png" and all works fine.

The Qt documentation states that AT THIS TIME they do not support true resource handling that Windows and Mac supports, but that things could change in the future. Remember, "true resource support" refers to that nice thing where you can place multi-megabyte graphics and sounds within your executable and never worry about them loading into memory until the magic time you request them. The QRC resources are loaded into memory when the executable is loaded up. The skinning by loadable file support more than makes up for this weakness. Qt is improving rapidly and one day they could very well support both the Windows and Mac resource systems. I am getting redundant and must go. Ta ta. Everybody mod me up, I need the points.

梦太阳 2024-08-17 08:05:20

也许您应该尝试以下操作:

在 .pro 项目文件中添加一行:

RC_ICONS = myappico.ico

更多信息:设置应用程序图标

Maybe you should try this:

add a single line to your .pro project file:

RC_ICONS = myappico.ico

More Information: Setting the Application Icon

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